Solved

preg_replace problem

Posted on 2008-06-22
3
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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.

751 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