Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 890
  • Last Modified:

PHP regular expression search and replace remove URL links from data

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
Darrennew
Asked:
Darrennew
  • 2
2 Solutions
 
marchentCommented:
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
 
DarrennewAuthor Commented:
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
 
DarrennewAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now