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.
Related
This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 8 years ago.
I'm using PHP 5.2.17. I want to remove some surplus data from a JSON string and I've thought I can use some replace function to do so. Specifically I'm using ereg_replace with the next expression:
'^.*?(?=\"created_at)'
Which I've validated at http://www.regexpal.com. I've pasted my JSON string in there and the match is right. However, when I make the call:
$tweets = eregi_replace('^.*?(?=\"created_at)', $temp, 'something');
and then I echo the $tweets variable, there's output. No errors in console neither. Apache error log, however, complains about an error called REG_BADRPT. There's a comment in the php docs of eregi_replace suggesting this can be due to I need to escape special characters, but I've already escaped the " character. And I've tried to escape others but no different behavior.
Where could the problem be then?
I don't think that ereg supports lookarounds. preg_replace exists in php 5.2, so you should really use that instead. It will work with your expression with delimiters.
$tweets = preg_replace('#^.*?(?=\"created_at)#i', 'something', $temp);
As other people have pointed out, ereg functions are deprecated, so use preg_replace. You also have to encapsulate your regex string in slashes (/). You can put your regex flags after your last slash.
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.
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).
This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
Dear Sir/m'am
How can i replace ther deprecated ereg_replace with preg_replace or str_replace
and still have the same functionality as in the code below?
return ereg_replace("^(.*)%%number%%(.*)$","\\1$i\\2",$number);
///this doesnt work
return preg_replace("^(.*)%%number%%(.*)$","\\1$i\\2",$number);
Anyone smarter have a clue?
Try this:
return ereg_replace("^(.*)%%number%%(.*)$","\\1$i\\2",$number);
becomes
return preg_replace("/^(.*)%%number%%(.*)$/","\\1$i\\2",$number);
Note the / around the regex.
I'll go with a read the fabulous manual approach.
The PHP Manual has a section for moving from POSIX Regex to PCRE.
The PCRE functions require that the pattern is enclosed by delimiters.
Unlike POSIX, the PCRE extension does not have dedicated functions for
case-insensitive matching. Instead,
this is supported using the /i pattern
modifier. Other pattern modifiers are
also available for changing the
matching strategy.
The POSIX functions find the longest of the leftmost match, but
PCRE stops on the first valid match.
If the string doesn't match at all it
makes no difference, but if it matches
it may have dramatic effects on both
the resulting match and the matching
speed. To illustrate this difference,
consider the following example from
"Mastering Regular Expressions" by
Jeffrey Friedl. Using the pattern
one(self)?(selfsufficient)? on the
string oneselfsufficient with PCRE
will result in matching oneself, but
using POSIX the result will be the
full string oneselfsufficient. Both
(sub)strings match the original
string, but POSIX requires that the
longest be the result.
Good luck,
Alin
Perl Compatible Regular Expressions, used by the preg_ functions in PHP require a demarcation character in the pattern string, defining where the actual string pattern starts and ends, and where attributes for extra functionality, such as case insensitivity, is.
For example:
$pattern = "/dog/i"; // Search pattern for "dog", case insensitive.
$replace = "cat";
$subject = "Dogs are cats.";
$result = preg_replace($pattern, $replace, $subject);
This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm a newbie with regular expressions and i need some help :).
I have this:
$url = '<img src="http://mi.url.com/iconos/oks/milan.gif" alt="Milan">';
$pattern = '/<img src="http:\/\/mi.url.com/iconos/oks/(.*)" alt="(.*)"\>/i';
preg_match_all($pattern, $url, $matches);
print_r($matches);
And I get this error:
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'c'
I want to select that 'milan.gif'.
How can I do that?
If you’re using / as delimiter, you need to escape every occurrence of that character inside the regular expression. You didn’t:
/<img src="http:\/\/mi.url.com/iconos/oks/(.*)" alt="(.*)"\>/i
^
Here the marked / is treated as end delimiter of the regular expression and everything after is is treated as modifier. i is a valid modifier but c isn’t (see your error message).
So:
/<img src="http:\/\/mi\.url\.com\/iconos\/oks\/(.*)" alt="(.*)"\>/i
But as Pekka already noted in the comments, you shouldn’t try to use regular expressions on a non-regular language like HTML. Use an HTML parser instead. Take a look at Best methods to parse HTML.
The problem is that you haven't escaped the forward slashes in the url string (you have escaped the ones in the http:// part, but not the url path).
Therefore the first one it comes across it (which is after .com), it thinks is the end of the regex, so it treats everything after that slash as the 'modifier' codes.
The next character ('i') is a valid modifier (as you know, since you're actually using it in your example), so that passes the test. However the next character ('c') is not, so it throws an error, which is what you're seeing.
To fix it, simply escape the slashes. So your example would look like this:
$pattern = '/<img src="http:\/\/mi.url.com\/iconos\/oks\/(.*)" alt="(.*)"\\>/i';
Hope that helps.
Note, as someone has already said, it's generally not advisable to use regex to match HTML, since HTML can be too complex to match accurately. It's generally preferrable to use a DOM parser. In your example, the regex could fail if the alt attribute or the end of the image URL contains unexpected characters, or if the quoting in the HTML code isn't as you expect.