Two quick ereg_replace() questions...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Two quick ereg_replace() questions...

Post by Gen-ik »

I'm finally starting to get my head around the ereg type stuff (as always it's easy once you know how to do it). I'm stuck one thing though at the moment.

How would I get an ereg search to look for either THIS or THAT?
For example here I'm trying to get the replacement to work if an optional blank space but not a quote exists before the http://

Code: Select all

<?php
$text = "xxxxhttp://www.somewhere.com http://www.somewhere.co.uk <a href="http://www.somewhere.net">link</a>";

$searchFor = "([\\s]?[^"])http://([^\\s]+)";

$replaceWith = "\\1<a href="http://\\2">\\2</a>";

$text = eregi_replace($searchFor, $replaceWith, $text);
?>
$text should end up looking like this..

xxxx<a href="http://www.somewhere.com">http://www.somewhere.com</a> <a href="http://www.somewhere.co.uk">http://www.somewhere.co.uk</a> <a href="http://www.somewhere.net">link</a>


Cheers.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

try this:

Code: Select all

$searchFor = "([^"])http://(([[]]|[[]])+)";
It works for your examples.
Post Reply