Php String query

Currently reading:
Php String query

mehstg

GPS Multijet 1.9 Orange
Joined
Aug 26, 2008
Messages
1,665
Points
288
Location
North London
Wondering if there is anyone on here who programs in PHP.

Basically, I have a PHP script that takes an input in from an XML stream. It then parses the XML file to variables.

The problem I am having, is, the <description> tag in the XML contains a load of extra data, surplus to my needs. I was wondering if anyone knew of a way in PHP to search for a character and remove everything after, and including the character. The surplus data always starts with "<"

Cheers

Paul
 
Will that substring only appear once? (so ALWAYS the first occurence of it is the place to chop)

Yup. Basically, it is the first < of the formatting. I just need the plain text preceding the formatting.
 
Ok there is no error checking in this, so that character will always need to be there.

$variable is the incoming var.

PHP:
$variable = substr($variable, 0, strpos($variable, "<")-1);

If there might be an occasion where that lt string WON'T be in the string, then use:
PHP:
if (strpos($variable, "<"))
{
$variable = substr($variable, 0, strpos($variable, "<")-1);
}

OR you could use a regex, but it's kinda overkill for this ;)
 
That's brilliant Ben, cheers. Really need to brush up on my PHP. Has been so long since I programmed it, it's like starting from scratch!
 
Back
Top