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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eawade
Forum Newbie
Posts: 1
Joined: Tue Dec 29, 2009 12:08 am

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

Post 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
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

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

Post by pbs »

User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply