CodeIgniter Forums
pre_replace not sure what the correct expression is - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: pre_replace not sure what the correct expression is (/thread-22814.html)

Pages: 1 2


pre_replace not sure what the correct expression is - El Forum - 09-21-2009

[eluser]sophistry[/eluser]
this might help. i renamed the word_censor function (as it was posted in the linked thread) to regex_mill()

i believe that this demonstrates almost perfectly what you are looking for:
Code:
$s = 'this is a <a href="string">string</a> with text/texts and HTML anchor and img tags (img tag here: <img src="">) mixed in. this is a string without a tag.';
$lookfor = array('(\s)(string)(\s)', 'texts?', 'HTML anchor', '[^<](img)');
$replacements = array('$2<a href="tags/$3">$3</a>$4','<a href="tags/$1">$1</a>','<a href="tags/$1">$1</a>','<a href="tags/$2">$2</a>');

$out = regex_mill($s, $lookfor, $replacements, 'i');
print_r(htmlentities($out));

to understand and use this regex_mill you are going to have to learn some regex to "tune" what the patterns find and what they are replaced with, but this should get you going with the basics.

happy birthday!


pre_replace not sure what the correct expression is - El Forum - 09-22-2009

[eluser]bugboy[/eluser]
Thank you so much,

I'll give it a go and see what i'll come upi


pre_replace not sure what the correct expression is - El Forum - 09-22-2009

[eluser]bugboy[/eluser]
your a star this is working great so far.


pre_replace not sure what the correct expression is - El Forum - 09-22-2009

[eluser]bugboy[/eluser]
Hi I've got it working with single words but when i come across a multiple words it stops working.

I've been reading that regex ref site you linked me too which great.

i think it regex to stop a string with multiple words appearing in a link would be

Code:
[^<](wordone wordtwo)

But it stops working completely.


pre_replace not sure what the correct expression is - El Forum - 09-22-2009

[eluser]sophistry[/eluser]
it depends which part of the "link" you are referring to. the href attribute or the display text that shows in between the open and close "a" tags?

which one of these:
Code:
<a href="this/is/a/path/and/shouldnothavespaces">display text with spaces</a>

if it is the display text, you just need to turn your less-than into a greater-than:

Code:
[^>](wordone wordtwo)

note: this assumes no space between the greater-than sign and the string "wordone".

cheers.


pre_replace not sure what the correct expression is - El Forum - 09-22-2009

[eluser]bugboy[/eluser]
Once again your a star.

Cheers for that