Omi
Omi

Reputation: 4007

Compare two strings ignoring white spaces, new lines, breaks

I want to compare two string & highlight matching continuous 4 or more words.

I am getting problem when there is double space or new line in string, string is not highlighting.

E.g. Suppose string1 is Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs.

String2 is laying out print, graphic

Expected Output:

Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in <span style="color:red">laying out print, 

graphic</span> or web designs.

PHP Code:

<?php
$str1 ="Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, 
graphic or web designs.";

$str2 = "/laying out print, graphic/iu";

echo preg_replace($str2, '<span style="color:red">$0</span>', $str1);
?>

Note: String1 formatting should be same.

Here is complete code : https://3v4l.org/3pBFR In this example it should highlight last paragraph from $answer but it is not highlighting.

Upvotes: 1

Views: 1072

Answers (1)

Nick
Nick

Reputation: 147216

One way to do this is to replace any whitespace in $str2 with \s+, which will then match any sequence of whitespace in $str. That can then be used in preg_replace as you are currently doing:

$str1 ="Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, 
graphic or web designs.";

$str2 = "laying   out
print, graphic";

$regex = preg_replace('/\s+/', '\s+', $str2);

echo preg_replace("/$regex/iu", '<span style="color:red">$0</span>', $str1);

Output:

Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in <span style="color:red">laying out print,
graphic</span> or web designs.

Demo on 3v4l.org

Note (as pointed out in the comments), it is safest to use preg_quote on the replacement string, to allow for the case where it may contain regex special characters, or possibly the delimiter used in the final preg_replace. For example:

$str1 ="Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, 
<i>graphic</i> or web designs.";

$str2 = "laying   out
print, <i>graphic</i>";

$regex = preg_replace('/\s+/', '\s+', preg_quote($str2, '/'));

echo preg_replace("/$regex/iu", '<span style="color:red">$0</span>', $str1);

Without the preg_quote, the second preg_replace will fail with an "unknown modifier '>'" error.

Output:

Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in <span style="color:red">laying out print, 
<i>graphic</i></span> or web designs.

Demo on 3v4l.org

Upvotes: 3

Related Questions