473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

preg_match not working

Hello everyone,

I am trying to use preg_match for replacing a string.
I am using following code

Expand|Select|Wrap|Line Numbers
  1. $regExp = "/^[i]$/";
  2. $match = "hii this";
  3. $result = preg_replace($regExp,"s",$match);
  4. echo $result ; // results into hii this
The preg_replace is not replacing the matching character.
But if i use $regExp = "/[i]/"; instead of $regExp = "/^[i]$/";
it works ,Can I get the reason behind this .I guess the
$regExp = "/^[i]$/"; should also be work.The caret(^) and the dollar($) just specify the start and end of the regular expression.


Thanks
Mar 26 '11 #1
2 997
dgreenhouse
250 Expert 100+
This '$regExp = "/^[i]$/";' is telling the regex to match the character class [i] at the beginning and end of the subject string only. And this '$regExp = "/[i]/";' is telling it to match anywhere in the subject string.
Mar 26 '11 #2
Thank you dgreenhouse for your quick reply. I get the thing now.
Thank you
Mar 27 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&" preg_match_all ("|product=(\d{5,10})&|i", $html, $out); $out...
4
by: DH | last post by:
I need to parse some HTML tags and display the style classes, and have it partly working, but need some regex / preg_match advise. If the tag is <td class="red" colspan="1"> I can display...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
5
by: Andrew Richardson | last post by:
Apologies if this has been asked before - I can't find anything on Google or Google Groups. I am running PHP 5.0.4 on Apache 2.0.54 with the PCRE extension installed. For some reason though, the...
2
by: Jørn Dahl-Stamnes | last post by:
I try to use preg_match to find out if a string begins with either "http", "/somethime" or "../something". In my php script I have the following line: if (0 ==...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
1
by: aaronic | last post by:
I took a script off another site for resizing an image using GD Tools. It works properly keeping the aspect ratio the same except for when the source image is a square, and the thumbnail you want to...
8
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.