Solved

preg replace for links

Posted on 2009-05-08
2
455 Views
Last Modified: 2012-08-14
hello there,
I have a little function for links to replace them from a string and its working fine..
I would like to add something so that if there is an image inside the link to ignore the replace
for example this code does this




$post = preg_replace('/\[url="?([^\]"]+)[\]"].*/i', '$1', $post);

 

[URL="http://google1.com"]google is a search engine[/URL]

[URL=http://google2.com]google is a search engine[/URL]

[URL=http://googleimg.com][IMG]http://google.com/logo.jpg[/IMG][/URL]

 

into

 

http://google1.com

http://google2.com

http://googleimg.com

 

 

im trying to do something so if there is [img] tags inside the [url] to NOT do a replace...

how can I do that?

Open in new window

0
Comment
Question by:XK8ER
  • 2
2 Comments
 
LVL 8

Expert Comment

by:fcardinaux
ID: 24342537
Is $post a text where there can be any number of links ?

If not, if each $post only consists in one link, you can do this:

<?php
 

$posts = array(

    '[URL="http://google1.com"]google is a search engine[/URL]',

    '[URL=http://google2.com]google is a search engine[/URL]',

    '[URL=http://googleimg.com][IMG]http://google.com/logo.jpg[/IMG][/URL]');
 

foreach ($posts as $post) {

    

    if (false === stripos($post, '[img]')) {

        $post = preg_replace('/\[url="?([^\]"]+)[\]"].*/i', '$1', $post);

    }

    

    echo "$post<br>\n";

}

 

?>

Open in new window

0
 
LVL 8

Accepted Solution

by:
fcardinaux earned 500 total points
ID: 24342626
In the other situation, you need to start by transforming the image links into a different, unmatchable pattern, then to replace the URLs, and finally to transform the image links back to their original pattern.
<?php
 

$post = <<<ENDOFPOST

Everybody knows that [URL="http://google.com"]google dot com[/URL] is a search engine, and 

that [URL=http://yahoo.com]yahoo dot com[/URL] is a search engine as well. Here is google's

logo: [URL=http://googleimg.com][IMG]http://google.com/logo.jpg[/IMG][/URL]. Enjoy!

ENDOFPOST;
 

$post = preg_replace('/\[url="?([^\]"]+)"?\]\[img\]([^\[]*)\[\/img\]\[\/url\]/i', '{~I~$1~M~$2~G~}', $post);

$post = preg_replace('/\[url="?([^\]"]+)"?\][^\[]*\[\/url\]/i', '$1', $post);

$post = preg_replace('/\{~I~([^~]*)~M~([^~]*)~G~\}/i', '[URL="$1"][IMG]$2[/IMG][/URL]', $post);

    

echo "$post<br>\n";
 

?>

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

18 Experts available now in Live!

Get 1:1 Help Now