954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

matching a simple 2 letters in a string

I have a string
$initial_string = ""

In this I want to match the letters 'cb' and replace it with '1cb'.
The final string should look like this
$final_string = ""

My code is as follows. But nothing works. I have tried all tricks. Help will be greatly appreciated.

if (preg_match("cb", $initial_string))
{
$pattern = "cb";
$pat = "1cb";
$replacement =  "$pat";
$final_string = preg_replace($pattern, $replacement, $initial_string);
echo "$final_string";
}
raghujosh
Junior Poster
109 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

The $pattern must be a regular expression. It must have delimiters around the pattern, and optional modifiers after the second delimiter.

Example patterns:

"/cb/" - matches cb
"/cb/i" - matches upper and lower case cb

see the docs for preg_match. http://php.net/preg_match

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

The $pattern must be a regular expression. It must have delimiters around the pattern, and optional modifiers after the second delimiter.

Example patterns:

"/cb/" - matches cb "/cb/i" - matches upper and lower case cb

see the docs for preg_match. http://php.net/preg_match


Thanks.It worked like charm

raghujosh
Junior Poster
109 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You