How to get specific snippets of a string in php

0

I'm using an adapted API that takes me back to the following data in XML

<?xml version="1.0" encoding="utf-8" ?>
<ajax>
    <cmd p="innerHTML" t="shurlmsg">
        <![CDATA[<div class="myWinLoadSD">This link is already on your list</div>]]>
    </cmd>
    <cmd p="js">
        <![CDATA[
           $('#shurlin').removeClass('ajaxloading');
           setTimeout("$('#shurlmsg').html('');", 4000);
           $('#noresult').remove();
           $('#urls').prepend('');
           $('#shurlin').val('');
           $('#shurlout').val('http://example.com/Um5-Cg').show().focus().select();
        ]]>
</cmd>

What I want is to get the URL shortened http://example.com/Um5-Cg .

    
asked by anonymous 17.02.2015 / 18:21

1 answer

2

You can get the URL using regular expressions.

$xml = <<<EOT
<?xml version="1.0" encoding="utf-8" ?>
<ajax>
    <cmd p="innerHTML" t="shurlmsg">
        <![CDATA[<div class="myWinLoadSD">This link is already on your list</div>]]>
    </cmd>
    <cmd p="js">
        <![CDATA[
           $('#shurlin').removeClass('ajaxloading');
           setTimeout("$('#shurlmsg').html('');", 4000);
           $('#noresult').remove();
           $('#urls').prepend('');
           $('#shurlin').val('');
           $('#shurlout').val('http://example.com/Um5-Cg').show().focus().select();
        ]]>
</cmd>
EOT;

preg_match("/\('#shurlout'\).val\('(https?:\/\/.*\/\S*)'\)/i", $xml, $resultado);

print_r($resultado);

echo $resultado[1];

Result:

Array
(
    [0] => ('#shurlout').val('http://example.com/Um5-Cg')
    [1] => http://example.com/Um5-Cg
)
http://example.com/Um5-Cg
    
17.02.2015 / 19:57