Edit: <facepalm X 1000> Be sure to use double quotes in the actual preg functions so the $reg variable interpolates. Original posting left for stupidity's sake . . . . ----------------------------------- Odd! dublinmike's solution should work, and works perfectly in Perl: (wrong . . . also works in PHP) #!/usr/bin/perl $reg = '[^\s]{25,}'; $replace_string = '<strong style="color:#ff0000">This word is too long</strong>'; $str = ' <p>this is a simple string of cxdhbnmkgfhgfsa/:$&@xxstrehxvifdsa that I want to check</p> <p>Slightly modifed for multiline testing.</p> '; print "<p>regex: $reg</p> $str\n"; if ($str =~ /$reg/m) { $str =~ s/$reg/$replace_string/m; print "<p><strong>Warning:</strong> you have a string that is too long.</p>\n"; } print $str; ######### End perl
However, does not work in PHP! (wrong . . . yes it does . . . ) <?php header("content-type:text/html"); $reg = '[^\s]{25,}'; $replace_string = '<strong style="color:#ff0000">This word is too long</strong>'; $str = ' <p>this is a simple string of cxdhbnmkgfhgfsa/:$&@xxstrehxvifdsa that I want to check</p> <p>Slightly modifed for multiline testing.</p> '; echo "<p>regex: $reg</p> $str\n"; if (preg_match("/$reg/m",$str)) { $str = preg_replace("/$reg/m",$replace_string,$str); echo "<p><strong>Warning:</strong> you have a string that is too long.</p>\n"; } echo $str; ?>
|