Solved

PHP regular expression search and replace remove URL links from data

Posted on 2009-06-29
3
871 Views
Last Modified: 2013-12-13
I am trying to get help i need to remove URL links from data before importing into mysql i need to strip links from a function i new to php so i don't know much and after 6 hours of trying to learn myself i have been unsuccessfully

Example:

<a href="test.com">test link</a>
www.test1.com, test2.co.uk, test.2.au and so on.

I also need to be able to strip a <p></p> tag but preserve data between the tag.

I know i can use strip_tags but i want to allow certain tags but any hidden links must be removed any idea where i can start

Many thanks
0
Comment
Question by:Darrennew
  • 2
3 Comments
 
LVL 13

Assisted Solution

by:marchent
marchent earned 100 total points
ID: 24739232
This could be an idea for you. You can update the code for more things to handle.
$str = '<a href="test.com">test link</a>

www.test1.com, test2.co.uk, test.2.au and so on.

<p> hellow</p> and <p > </p>

';
 

$str = preg_replace('/<a[^>]*href[^>]*>([^<]*)<\/a>/i', '$1', $str); // remove links

$str = preg_replace('/<p[^>]*>|<\/p>/i', '', $str); // replace <p> tag
 

print $str;

Open in new window

0
 
LVL 1

Author Comment

by:Darrennew
ID: 24739401
Hi that worked for <a> tag and <p> tag what i need to do is remove domain names and email links from the data too this is what i just can't work out.

test@localhost.com  www.localhost.com www.test.co.uk  www.test.au

so i need a method of removing all of the above i tried to move things around but my lack of knowledge and understanding how the expressions work.

can you point me in right direction please

Kind Regards
0
 
LVL 1

Accepted Solution

by:
Darrennew earned 0 total points
ID: 24842915
Hi not much help here apart from one expert i googled it and found regexbuddy software which helped me to create regex this tool is very good i was i am tring to learn PHP and regex patterns lots of help came with this program and helped me create what i needed.

Kind Regards to all
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
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…
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 …

746 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

9 Experts available now in Live!

Get 1:1 Help Now