Page 1 of 1

Need to search a string for a substring of a certain length

Posted: Tue Dec 29, 2009 12:09 am
by eawade
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

Re: Need to search a string for a substring of a certain length

Posted: Tue Dec 29, 2009 12:14 am
by pbs

Re: Need to search a string for a substring of a certain length

Posted: Tue Dec 29, 2009 12:33 pm
by AbraCadaver
eawade wrote: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
One way:

Code: Select all

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