Forum Moderators: coopster
Example:
$str = "this is a simple string of cxdhbnmkgfhgfsa/:$&@xxstrehxvifdsa that I want to check";
I want to be able to locate the long substring within $str and replace it with something else. Any help will be appreciated.
Thanks,
Ethan
-----------------------------------
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;
?>