0

I’m having a string which is autogenerated by a third party web-service, where I want to get the URL from the string. The string looks like this:

'document.write("<div class=\"display_archive\"><div class=\"campaign\">20\/12\/2011 - <a href=\"http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3\" title=\"News\" target=\"_blank\">News<\/a><\/div><\/div>");'

I want to retrieve the URL, in above case this URL: http://us2.campaign-archive1.com/?u=fdf89fgd7sdf7d8&id=ffd89dfef3\ and I want to remove escape backslashes so the URL is: http://us2.campaign-archive1.com/?u=fdf89fgd7sdf7d8&id=ffd89dfef3/

I’ve tried with some different parsers and Regex, but I’m not that strong in Regex and can’t seem to get the URL correctly. I tried this preg_match but it doesn’t work and is only returning empty arrays:

%^((http?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i

Any help is much appreciated.

Sincere
- Mestika

3 Answers 3

1

Try this:

<?php
$response = 'document.write("<div class=\"display_archive\"><div class=\"campaign\">20\/12\/2011 - <a href=\"http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3\" title=\"News\" target=\"_blank\">News<\/a><\/div><\/div>");';

preg_match('/href=\\\\\"([^\"]+)/', $response, $matches);

echo 'Raw URL: ' . $matches[1] . '<br />';
echo 'Clean URL: ' . stripslashes($matches[1]);
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Your regex is not working becaus of slashes. Just pass the string by stripslashes() and then apply regex

Comments

0

have you tried str_replace(). e.g)

$url = "http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3";
$url = str_replace('\\', '', $url);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.