1

I am using regular expressions with preg_replace() in order to find and replace a sentence in a piece of text. The $search_string contains plain text + html tags +   elements. The problem is that only sometimes the   elements convert to white space on run time, making it difficult to find and replace using str_replace(). So, I'm trying to build a pattern that is equal to the search string and will match anything like it which contains, or does not contain the   elements;

For example:

$search_string = 'Two years in,&nbsp;the company has expanded to 35 cities, five of which are outside the U.S. Plus,&nbsp;in&nbsp;April, <a href="site.com">ClassPass</a> acquired its main competitor,&nbsp;Fitmob.';

$pattern = $search_string(BUT IGNORE THE &nbsp; elements in the subject)

$subject = "text text text text text". $search_string . "text text text text text";

Using A regular expression to exclude a word/string, I've tried:

     $pattern = '`^/(?!\&nbsp;)'.$search_string.'`';
     $output = preg_replace($pattern, $replacement_string,$subject);

The end result will be that if the $subject does contains a string that is like my $seach_string but without the &nbsp; elements, it will still match and replace it with $replacement_string

EDIT:

The actual values:

$subject = file_get_contents("http://venturebeat.com/2015/11/10/sources-classpass-raises-30-million-from-google-ventures-and-others/");

 $search_string = "Two years in,&nbsp;the company has expanded to 35 cities, five of which are outside the U.S. Plus,&nbsp;in&nbsp;April, ClassPass acquired its main competitor,&nbsp;Fitmob."; 

$replacement_string = "<span class='smth'>Two years in,&nbsp;the company has expanded to 35 cities, five of which are outside the U.S. Plus,&nbsp;in&nbsp;April, ClassPass acquired its main competitor,&nbsp;Fitmob.</span>"; 
12
  • IGNORE or convert to space? Commented Nov 12, 2015 at 12:54
  • I've tried that and it doesn't work. May be I'm wrong - the str_replace() fails not because of &nbsp; but something else. It's worth mentioning that the $subject is actually a whole web page. Any other suggestions? It seems I need a regex pattern that will match any string in the subject which has, say 90% similarity to the $search_string ? Commented Nov 12, 2015 at 12:55
  • Please, could you give the link to the source page and the real search and replacement strings? Commented Nov 12, 2015 at 12:58
  • 1
    $subject = file_get_contents("venturebeat.com/2015/11/10/…); $search_string = "Two years in,&nbsp;the company has expanded to 35 cities, five of which are outside the U.S. Plus,&nbsp;in&nbsp;April, ClassPass acquired its main competitor,&nbsp;Fitmob."; $replacement_string = "<span class="smth">Two years in,&nbsp;the company has expanded to 35 cities, five of which are outside the U.S. Plus,&nbsp;in&nbsp;April, ClassPass acquired its main competitor,&nbsp;Fitmob.</span>"; Commented Nov 12, 2015 at 13:07
  • 1
    Would it make anything easier to first do a $subject = str_replace( '&nbsp;', ' ', $subject ); and the do the search when it only contains ordinary spaces? Commented Nov 12, 2015 at 22:46

1 Answer 1

0

Not a very efficient way of doing it but it should workout for you,

preg_replace('Two.*?years.*?in.*?the.*?company.*?has.*?expanded.*?to.*?35.*?cities.*?five.*?of.*?which.*?are.*?outside.*?the.*?U\.S\..*?Plus.*?in.*?April.*?ClassPass.*?acquired.*?its.*?main.*?competitor.*?Fitmob\.', '<span class=\'smth\'>$0</span>', $subject);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.