Solved

preg_replace problem

Posted on 2008-06-22
3
280 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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