homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Need to search a string for a substring of a certain length
Search for substring of continuous characters
eawade




msg:4050663
 6:06 am on Dec 29, 2009 (gmt 0)

I am trying to create a simple function, maybe using Regular Expressions, that would let me search text for a substring of continuous characters. For example, I don't want a word or URL etc. to exceed 25 characters in length. The most important thing is that the string is identified and replaced.

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

 

dublinmike




msg:4050762
 12:45 pm on Dec 29, 2009 (gmt 0)

Maybe something like:

$str = preg_replace('/[^ ]{25,}/','',$str);

Haven't tested (or re-read it) but it should get you on the right track...

rocknbil




msg:4051000
 8:34 pm on Dec 29, 2009 (gmt 0)

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;
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved