Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP ereg_replace problem

Status
Not open for further replies.

balf

Programmer
Feb 9, 2005
4
GB
I have text of the form

"<a href=a.b.c>Link 1</a> here are more words <a href=x.y.z>Link 2</a>"

which I need to change into BBcode:

"Link 1 here are more words Link 2"

What do the regular expressions parameters need to be to accompish this - way I try it gives me:

"Link 1</a> here are more words <a href=x.y.z>Link 2"

this looks as if I am matching everything from the first "<a href" to the last "</a>" and ignoring the other two tags.
 
thats called the GREEDY mode of RegEx, u have to change it to the ungreedy mode. u can set that by:
a. setting the /U identifier. (like i,g)

b. if u have any .+ change that to .+? (also applicable for .*)

c. dont use .+ instead try to match a [^>]. e.g: instead of <a.+> use a<[^>]+>

Known is handfull, Unknown is worldfull
 
Hi,

try next code I'm ussing this for years.
Plus a feew tags available.

Code:
$bb_reverse = array(
        'patterns' => array(
                '/<br>/i',
                '/<b>/i', '/<\/b>/i',
                '/<i>/i', '/<\/i>/i',
                '/<u>/i', '/<\/u>/i',
                '/<a href="http:\/\/([^>]+)" target="_blank">([^<]+)<\/a>/i',
                '/<ul>/i', '/<\/ul>/i', '/<li>/i',
                '/<font color="([^"]+)">/i', '/<\/font>/i',
                '/<img src="([^"]+)">/i',
                '/<a href="mailto:([^"]+)">([^<]+)<\/a>/i',
                '/<a href="http:\/\/([^>]+)" target="_blank">(.*?)<\/a>/i',
                '/<embed(.*?)src="([^"]+)">/i',
                ),
        'replaces' => array(
                '[br]',
                '[ b]', '[ /b]',
                '[ i]', '[ /i]',
                '[ u]', '[ /u]',
                '[url=http://$1]$2[/url]',
                '[list]', '[/list]', '[*]',
                '[color:$1]', '[/color]',
                '[image]$1[/image]',
                '[email]$1[/email]',
                '[url=http://$1]$2[/url]',
                '[embed$1]$2[/embed]',
                )
        );

$rev_text = preg_replace($bb_reverse['patterns'], $bb_reverse['replaces'], $text);




Pls cut the speace where is [ b], [ i], [ u];
also from [ /b], [ /i], [ /u].

I had to post like that (with a space) because otherway tek-tips forum will replace my original code with BB tags :)


Regards



___
____
 
Specifically I need to use the eregi_replace() function 'cos that's what my client is using.

1. You use the "i" postfix - how do you specify it when using eregi_replace(). I mean, does it tag onto the end of the first or the second parameter?

2. The manual I got seems to indicate that captured subsections can be used via the //1,//2...//n terms - you appear to be using $1,$2...$n. Is there a difference?

3. if I read your code correctly

"<a href=a.b.c>Link 1</a> here are more words <a href=x.y.z>Link 2</a>"

can be changed into BBcode:

"Link 1 here are more words Link 2"

if I use the following regex:

'/<a href="([^>]+)>([^<]+)<\/a>/i'

as the first(?) parameter to eregi_replace?
 
The regex at the end of my last post: 'href="' should of course be 'href='

sorry
 
yup the RegEx:
<a href="([^>]+)>([^<]+)<\/a>

should do it, i hab\vent used modifiers in ereg but u could use them in preg_replace() function...

Known is handfull, Unknown is worldfull
 
OK will let you know what happens when I try to use it!!!
Thanks for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top