Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

preg_replace problem

Posted on 2008-06-22
3
Medium Priority
?
285 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

715 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