1

I am trying to extract a string from another string using php.

At the moment im using:

<?php
  $testVal = $node->field_link[0]['view'];
  $testVal = preg_replace("#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie", "'<a href=\"$1\" target=\"_blank\">$3</a>$4'", $testVal);
  print "testVal = ";
  print $testVal;
?>

This seems to be printing my entire string at the moment.

Now what i want to do is: extract a web address if there is one and save it as a variable called testVal.

I am a novice so please explain what i am doing wrong. Also i have looked at other questions and have used the regex from one.

For @bos

Input:

<iframe width="560" height="315" src="http://www.youtube.com/embed/CLXt3yh2g0s" frameborder="0" allowfullscreen></iframe>

Desired Output

http://www.youtube.com/embed/CLXt3yh2g0s
Undefined
  • 11,234
  • 5
  • 37
  • 62
  • You write that you want to find a link, but your regexp hints that you want to find an URL and turn it into a link. Which is it? – bos Mar 20 '12 at 16:27
  • I want to find a url and save it as a variable. It may be that the regex is wrong :/ – Undefined Mar 20 '12 at 16:31
  • Show test cases. Show indata, actual outdata and expected outdata. – bos Mar 20 '12 at 16:33
  • Possible duplicate of http://stackoverflow.com/questions/206059/php-validation-regex-for-url – cmbuckley Mar 20 '12 at 16:45

1 Answers1

0

Well, you say you want to populate $testVal with the extracted web address, but you're using preg_replace instead of preg_match. You use preg_replace when you wish to replace occurrences, and you use preg_match (or preg_match_all) when you want to find occurrences.

If you want to replace URLs with links (<a> tags) like in your example, use something like this:

<?php
$testVal = preg_replace(
     '/((?:https?:\/\/|ftp:\/\/|irc:\/\/)[^\s<>()"]+?(?:\([^\s<>()"]*?\)[^\s<>()"]*?)*)((?:\s|<|>|"|\.||\]|!|\?|,|&#44;|&quot;)*(?:[\s<>()"]|$))/',
     '<a target="_blank" rel="nofollow" href="$1">$1</a>$2',
     $testVal
);

If you want to instead simply locate a URL from a string, try (using your regex now instead of mine above):

<?php
$testVal = $node->field_link[0]['view'];
if(!preg_match("#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie", $testVal, $matches)) {
     echo "Not found!";
else {
     echo "URL: " . $matches[1];
}

When you use preg_match, the (optional) third parameter is filled with the results of the search. $matches[0] would contain the string that matched the entire pattern, $matches[1] would contain the first capture group, $matches[2] the second, and so on.

Savetheinternet
  • 491
  • 4
  • 11