1

I have preg_match problem. I should preg_match if xml text is something like this below.

<File label="asd 480p" type="lol" rate="1500" resolution="854x480">ValueIwant</File>

or this

<File label="720p" default="1">ValueIwant</File>

Now I'm using format like that

preg_match("'<File label=\"(?:720|576|cat|asd 480p).{1,50}>(.*?)</File>'si", $streamdata, $streamurl);

But it's working only for values like on my second example code < File label="720p" default="1">

sukkis
  • 312
  • 2
  • 17
  • [Regex is not the correct tool to parse XML](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) –  Sep 19 '15 at 10:07

2 Answers2

1
preg_match("'<File label=\"[(720)(576)(cat)(asd 480)].+>(.*)<\/File>'si", $streamdata, $streamurl);

The regex you posted should work too if you replace the {1,50} with either a higher number than 50 or just a plus sign (+).

Max
  • 207
  • 1
  • 6
  • 20
0

You are usually better off using an XML parser rather than preg_match to process XML. For example:

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML( '<xml>
    <File label="asd 480p" type="lol" rate="1500" resolution="854x480">ValueIwant</File>
    <File label="720p" default="1">ValueIwant</File>
    </xml>' ); 

$searchNode = $xmlDoc->getElementsByTagName( "File" ); 

foreach( $searchNode as $searchNode ) {
    $label = $searchNode->getAttribute('label');

    $value = $searchNode->nodeValue;

    echo "$label - $value<br>";
} 

Would output:

asd 480p - ValueIwant 720p - ValueIwant

Dean Jenkins
  • 194
  • 5