How to pick specific snippets of a php string

Asked

Viewed 194 times

0

I am using a adapted API that retrieves the following XML data

<?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 shortened URL http://example.com/Um5-Cg.

  • Do you want to capture the URL and shorten it then? Or did you mean "I want to get that shortened url," if that’s it then the @xsquirrel answer seems to be a way.

1 answer

2

You can pick up 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];

Upshot:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.