LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 06-19-2008, 08:00 AM   #1
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,042
Thanked: 14
PHP function to check if string is valid xml?


[Log in to get rid of this advertisement]
Hi,
we are using simplexml_load_string() function that expects a valid xml string as first argument to generate some output with php. Unfortunately not every input gathered and sent to the function is valid xml. Sometimes html or text. Is there a way to check whether a given string is valid xml in php? The files are not very big in general. Do you have an idea how to accompish that? Would be great if you let me know, thanks!
j
j-ray is offline  
Tag This Post , , ,
Reply With Quote
Old 06-19-2008, 08:08 AM   #2
sxeraverx
LQ Newbie
 
Registered: Mar 2008
Posts: 8
Thanked: 0
simplexml_load_string will return false if it's not a valid XML string. Go ahead and load it, and then just check the return value:

$a = simplexml_load_string($somestring);
if($a===FALSE) {
//It was not an XML string
} else {
//It was a valid XML string
}

Make sure to use ===, as some functions can return values that evaluate to false (like null, or an empty array, or an empty string), but you need to check for specifically false.
sxeraverx is offline     Reply With Quote
Old 06-19-2008, 08:09 AM   #3
rubadub
Member
 
Registered: Jun 2004
Posts: 226
Thanked: 0
Not off the top of my head, also can't think how you'd do it with regex either. Yet you can use this to find out what was wrong with it and act accordingly.

You could start looking here though...
rubadub is offline     Reply With Quote
Old 06-19-2008, 08:49 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,042
Thanked: 14

Original Poster
as you said simplexml_load_string will return false if the string is not valid xml but unfortunately it spits out a lot of warnings that we want to avoid. But thanks anyway! I had not noticed it was like that before ;-)
j-ray is offline     Reply With Quote
Old 06-19-2008, 09:03 AM   #5
rubadub
Member
 
Registered: Jun 2004
Posts: 226
Thanked: 0
@ supresses errors!


Use a try/catch clause, here's an example for mysql...
Code:
try
	{
		if ( !@ ( $conn = mysql_connect($db_host, $db_uname, $db_pass) ) )
			throw new Exception (mysql_error());
	}
	catch (Exception $e)
	{
		//echo 'ERROR: ' . $e->getMessage();
		return array(-1, $conn, "?");
	}
rubadub is offline     Reply With Quote
Old 06-29-2009, 03:34 PM   #6
ctrahey
LQ Newbie
 
Registered: Jun 2009
Posts: 2
Thanked: 0
You can supress

Without digging up a more robust solution, you can combine error suppression and the false check:

@$a = simplexml_load_string($somestring);
... check $a
ctrahey is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I check if the php function file() fails? farmerjoe Programming 6 12-02-2007 10:17 AM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 12:34 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 01:08 PM
optimize PHP string function Cedrik Programming 6 08-29-2004 10:07 AM
PHP script/function to get 4-5 words around a keyword in a string(like google) rmanocha Programming 8 07-13-2004 07:19 AM


All times are GMT -5. The time now is 03:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration