Jump to content

preg_replace question


jammmet

Recommended Posts

Hi,

I'm new to both php and regular expressions. I need to replace some stuff in a string, but am at a complete loss as to how to do it. Links in my database are entered like this:

 

[[externalLink1]here]

 

and I want it to be transformed to look like this:

 

<a href="externalLink1">here</a>

 

 

Below is what I have so far - obviously it doesn't work. Can anyone help me out? much appreciated:

 

<?php
$src = "Download DirectX from [[externalLink1]here] Download Windows Media Player from [[externalLink2]here]";

$urls[0] = "http://www.microsoft.com/windows/directx";
$urls[1] = "http://www.microsoft.com/windows/mediaplayer";

echo preg_replace ("/\[\[(externalLink.*)\](.*)\]/", $urls, $src);
?>

Link to comment
https://forums.phpfreaks.com/topic/56698-preg_replace-question/
Share on other sites

<pre>
<?php
$src = "Download DirectX from [[http://www.microsoft.com/windows/directx]here] Download Windows Media Player from [[http://www.microsoft.com/windows/mediaplayer]here]";
echo preg_replace ('/\[\[([^]]+)\]([^]]+)\]/', '<a href="\1">\2</a>', $src);
?>
</pre>

Link to comment
https://forums.phpfreaks.com/topic/56698-preg_replace-question/#findComment-280090
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.