This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Tilde operator in Regular expressions
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
The code is from http://php.net/manual/en/functions.anonymous.php
I searched for what "~" is in regex and did not find an answer.
What does it do?
The first and last character of a regular expression in PHP (and other implementations) is known as the delimiter. Normally, you see a / being used, but in this case, someone chose ~. Read more here.
Not sure why ~ was chosen though; probably a habit of that particular developer. Normally, one chooses a different delimiter over / when the regular expression itself will contain slashes (e.g. matching URLs), so that slashes don't need to be escaped every time.
The symbol ~ is just used as delimiter in PHP regexps.
Related
This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 2 years ago.
I found one PHP script for get a database backup, there use this expression to do something, that script show call to undefined function ereg_replace() this error, if i remove this line script is working fine...
how to replace this function $row[$j] = ereg_replace("\n","\\n",$row[$j]); to that script working finely,
Can anyone assist me..
ereg_replace deprecated and remove from newer versions of php (7 and up).
You will have to update your code to use preg_replace
$row[$j] = preg_replace("#\n#","\\n",$row[$j]);
One of the differences between ereg_replace() and preg_replace() is that the pattern must be enclosed by delimiters: delimiter + pattern + delimiter, in this case we are using # so we don't have to escape / that is the usual that is used.
Valid Delimiters:
/, #, ~, +, %, #, ! , <, >
Reference from php.net
hope it helps.
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
Given a fairly simple regex, I'd like to match a text between to delimiters:
___MANUAL_TICKET___
###_CLIENT_START_###
TEST
###_CLIENT_END_###
###_PROBLEM_START_###
TEST2
###_PROBLEM_END_###
###_EMAIL_START_###
xyz#test.com
###_EMAIL_END_###
To get the client I am using this regex:
###_CLIENT_START_###\s(.*?)\s###_CLIENT_END_###
which works as seen HERE.
But when I use it in my PHP Code it does not find any matches:
preg_match('####_CLIENT_START_###\s(.*?)\s###_CLIENT_END_####', $source, $matches);
(tried different regex delimiter such as / and ~, same result)
What am I doing wrong?
Note that the dot (.) by default matches any symbol but the line feed (See the documentation).
Since you have multiple line input, you need to use the PCRE_DOTALL option, which can be enabled just adding symbol s at the very end of the pattern
preg_match('####_CLIENT_START_###\s(.*?)\s###_CLIENT_END_####s', $source, $matches);
^ here
This question already has answers here:
regular expression and forward slash
(4 answers)
Closed 6 years ago.
Does anyone know why are we adding forward slash in front and back of pattern in preg_match method in PHP?
preg_match('/test/', "Welcome to testing world");
Also how to add them dynamically to pattern?
A delimiter is a special character used at the start and end of your expression to denote which part is the expression. This allows you to use modifiers and the interpreter to know which is an expression and which are modifiers.
Example:
preg_match('/Test/', "Welcome to testing world"); //nothing found
preg_match('/Test/i', "Welcome to testing world"); //i -> case-insensitive search, all ok
This question already has answers here:
What does the "~" character signify in PHP regex?
(3 answers)
Closed 6 years ago.
I have seen some examples about regular expressions with "#" on PHP, something like this:
preg_match ("#[a-zA-Z0-9]#i", $value, $occurrences);
I could see that this counts the number of matches of regular expression which were found in the $value variable, but I would like to be sure if the "#" is used for this specific case or what's the main function of "#" in regular expressions?.
Can you help me please?
These are custom delimiters for the regular expression pattern. The most common is /, in your case they were changed to #.
The benefit of custom delimiters has to do with escaping. This is best shown by example.
Consider:
preg_match ("/Some\/Path/i", $value, $occurrences);
Versus:
preg_match ("#Some/Path#i", $value, $occurrences);
However, on a personal note, I tend to avoid them as it makes the regular expression pattern, well, customized. Just use the standard / delimiters.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
So I have some strings of this type:
choose_from_library_something
choose_from_library_something2
choose_from_library_something3
...
And I need to search for choose_from_library_*
here is my regular expression that doesn't work:
}elseif (preg_match('choose_from_library_.*',$form_name)) {
What I'm doing wrong?
You need to add delimiters to your regex:
preg_match('/^choose_from_library_.*/',$form_name)
EDIT: Added an anchor ^ to the beginning of the regex, to avoid matching don't_choose_from_library_, etc.
You may be better off using explode and count that off instead in your situation.
else if ( count ( explode("_" , $form_name) ) == 4 ) {
}
To answer your question, you need to have slashes around your pattern
/pattern/
also, asterisk means none or more, so even if you had no text, you it would still match it. The same would hold true for explode (but you can see if there was an empty string in the last element and return that as false).