1

How can I extract the HTTP from this text?

"Text: Here's 2 free bonuses. http://blablabla.blabla/blabla "

But the URL can also be another one.

Finally

After I have the array, which contains usually just one URL, how can I add it to the above text exactly at the same position? But with HTML tag <a>

The results should look something like this:

"Text: Here's 2 free bonuses. 
<a href='http://blablabla.blabla/blabla'>http://blablabla.blabla/blabla</a> "
1

2 Answers 2

1

You can do that using regular expression, and more precisely preg_replace(). The matching expression can be something like :

$pattern = 'http://[a-zA-Z0-9\.\-/]+';

Of course, you can refine it for more precise matching, but this should do the trick. If you want to play with regex, have a look at regexpal, it's a great tool for testing. Then, you can perform the replace ($0 corresponds to the whole matched string :

preg_replace($pattern, '<a href="$0">$0</a>', $yourString);
Sign up to request clarification or add additional context in comments.

2 Comments

how can i used for an array without going throw a foor loop?
According to the doc (see the link in my answer), preg_replace() works with $yourString being an array of strings (returns an array).
1

This regex should do the trick:

$s= "Text: Here's 2 free bonuses. http://blablabla.blabla/blabla and some more text";
$s2= preg_replace('~(http://\S+)\b~', '<a href="$1">$1</a>', $s);
var_dump($s2);

2 Comments

how can i use this for an array ?
According to the docs the third parameter can be an array of strings to apply the regex to: us3.php.net/preg_replace

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.