LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 06-19-2008, 07:00 AM   #1
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
PHP function to check if string is valid xml?


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
 
Old 06-19-2008, 07:08 AM   #2
sxeraverx
LQ Newbie
 
Registered: Mar 2008
Posts: 8

Rep: Reputation: 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.
 
Old 06-19-2008, 07:09 AM   #3
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
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...
 
Old 06-19-2008, 07:49 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Original Poster
Rep: Reputation: 145Reputation: 145
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 ;-)
 
Old 06-19-2008, 08:03 AM   #5
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
@ 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, "?");
	}
 
Old 06-29-2009, 02:34 PM   #6
ctrahey
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Rep: Reputation: 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
 
Old 12-19-2009, 07:46 AM   #7
neelkamalpandey
LQ Newbie
 
Registered: Oct 2009
Location: Australia
Distribution: Ubuntu
Posts: 2

Rep: Reputation: 0
Thanks for the help

Thanks for the help i was trying to validate my xml query string that is passed for one of my api and this was useful there.

Could you provide me some examples on could i validate GMT in php.
 
  


Reply

Tags
filter, function, php, xml



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 09:17 AM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
optimize PHP string function Cedrik Programming 6 08-29-2004 09:07 AM
PHP script/function to get 4-5 words around a keyword in a string(like google) rmanocha Programming 8 07-13-2004 06:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:39 AM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration