ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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 ;-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.