?
Solved

Regular Expression

Posted on 2007-10-20
2
Medium Priority
?
235 Views
Last Modified: 2007-10-20
Hmm
I am trying to do an expression and use preg_replace to remove all urls, except some
ie

/<a(.*?)href=\"(.*?)(ebay.\.com)(.*?)\"(.*?)<\/a>/

Where that would remove all urls except ebay.
but it dosn't replace anything, how can I tell it to 'ignore' ebay.com?
0
Comment
Question by:wildzero
2 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 1000 total points
ID: 20116155
try this

function check_url($matches){
 
 
  $url = "";
 
  $array_ignore = array("ebay.com","google.com"); // will ignore ebay.com and google.com ---- add urls here
 
 $prepare_url = parse_url($matches[1]);
 
 if($prepare_url["host"]) {
 
       $url = str_replace("www.","",strtolower($prepare_url["host"]));
 
 }else{
 
       return false;
 }
 
  if(in_array($url,$array_ignore)) {
 
        return $matches[0];
 
  }else{
 
        return false;
 
  }
}


echo preg_replace_callback('@<a href="([^"]+)">[^<]+</a>@',"check_url",$string);
0
 
LVL 10

Author Comment

by:wildzero
ID: 20116335
Great!
Thats working perfect
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 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