Solved

preg_replace problem

Posted on 2008-06-22
3
277 Views
Last Modified: 2013-12-12
I'm getting an error:
 
Warning: preg_replace() [function.preg-replace]: Unknown modifier '(' in C:\Documents and Settings\Brian\Desktop\test-cake\app\webroot\test.php on line 11

Line 11 is the preg_replace() function call.

I'm trying to use back references to replace the url and re-write links so that I can force a target on links.

I'm very very very new to regular expressions, my experience level is about 2.5 hrs... ;)



Thanks.
Brian

$html = '<a href="http://www.google.com">Link</a><a target="_none" href="http://www.google.com"></a>< a style="" href="www.yahoo.com" t arget="_blank"     >as   dajsdj< /A >< A href="" target="_self">askjkajlsd<a><a href="test">sdsdfsfsdfs</a>';
 

/* remove newlines so we can use regex */

$html = str_replace("\n","",$html);
 

/* rewrite links to open in a new window and remove tags */

$html = preg_replace('<\s*(?i:a).*(?i:href=)"([^"]*)".*>(.*)<\s*/\s*(?i:a)\s*>', 

                     '<a href="$1" target="_blank">$2</a>',

                     $html);
 

echo $html;

Open in new window

0
Comment
Question by:BrianGEFF719
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 21843041
preg_replace('{<\s*(?i:a).*(?i:href=)"([^"]*)".*>(.*)<\s*/\s*(?i:a)\s*>}',
                     '<a href="$1" target="_blank">$2</a>'
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 21843049
Hi Ozo,
That does resolve the error; however, I'm only getting back the last link...


Ideas?
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 21843373
The issue was resolved by using a lazy quantifier (?) eg .*?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

2 Experts available now in Live!

Get 1:1 Help Now