This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I have kind of simple regexes that i am using.
It's so simple as:
"user/"
"user/[A-z0-9_-]"
These all work fine with ereg, but not preg.
How to convert it?
Thanks
It's most probably because your're missing the delimiters. Try this:
"~user/~"
"~user/[A-z0-9_-]~"
ereg('jpg$', $query)
Turns into:
preg_match('#jpg$#', $query)
Related
This question already has answers here:
preg_match and UTF-8 in PHP
(8 answers)
Closed 4 years ago.
I'm trying to do some validation exercises and I was wondering this code seems to return false always.
if (preg_match("/^[0-9]{7}$/", '1234567')) {
die('match');
}
I'm currently testing for full-width japanese characters/numbers. I'm wondering why it doesn't execute the die command. Any help is appreciated. Thank you in advance :)
Regex Match for 1234567 :
Example Code :
<?php
preg_match('/[1234567]+$/','1234567',$output);
preg_match('/[1-7]+$/','5123467',$output);
print_r($output);
?>
OR :
<?php
preg_match('/[1234567]{21}$/','1234567',$output);
preg_match('/[1-7]{21}$/','5123467',$output);
print_r($output);
?>
Related Links :
http://php.net/manual/en/regexp.reference.unicode.php
PHP Regex to accept only specific characters
PHP preg_replace with UTF-8 not working
preg_match and UTF-8 in PHP
Need regex for utf8 multilingual search query
PHP preg_func/u or mb_ereg_func for UTF8? (regular expressions with multi-byte-PCRE or multi-byte-POSIX?)
This question already has answers here:
Finding a character at a specific position of a string
(5 answers)
Closed 5 years ago.
I have a string in PHP code like this:
$string = "This is a PHP code";
I want to find a character at index 3 in above string. And output must be:
s
Is there any idea to achieve this goal ?
If I understood your question properly, you want character at 3rd index then write following line ;)
echo $string[3];
This question already has answers here:
Replace preg_replace() e modifier with preg_replace_callback
(3 answers)
Closed 6 years ago.
I use a preg_replace for replacing words in templates
echo preg_replace('~%(\w+)%~e', '$obj->$1', $template);
$obj is an array of searches, and $template is the file I am searching and replacing. So I am replacing keywords like %REPLACE%.
Now I don't really understand how it works, and apparently the e modifier has just been deprecated so I think I have to use the pref_replace_callback instead. We are upgrading to php7 soon and so I must replace this line in my code.
Now I have already looked on stack overflow and found answers to other peoples similar problems, unfortunately the answers don't help with this particular pattern. I don't understand how this works let alone how to get preg_replace_callback working. I have tried reading up on preg_replace, but really I don't understand how it works.
So I how do I change the above code to preg_replace_callback?
PS. I have searched for tutorials on preg_replace_callback, but nothing that explains what I have to do.
This is not a duplicate of that other question. It is a completely different preg_replace. The answer to the other question does not answer my question, and I have not got the know how to work it out, as I am not very good with the preg_replace keyword.
Pretty sure this will work:
echo preg_replace_callback('~%(\w+)%~',
function($m) use($obj) {
return $obj->{$m[1]};
},
$template);
This question already has answers here:
json_encode() escaping forward slashes
(4 answers)
Closed 7 years ago.
When i insert url like this
alliedpaint-001-site1.smarterasp.net/white.png
in mysql and encode it using json my url shows like this
alliedpaint-001-site1.smarterasp.net\/white.png
How i can solve this?
json_encode returns the string, it inserts "\" in order to avoid special interpretation.
you can do 2 things-
1) json_encode($mystring, JSON_UNESCAPED_SLASHES);
or
replace "\" with space using regex.
Hope this helps.
This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
I am working for a non-profit and i'm not an expert in PHP.
I need to replace the following code:
$status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $status);
When I attempt to modify it to preg_replace, I get an error every different way I try to exit the code.
This will do the job:
$statut = preg_replace('~[a-z]+://[^<>\s]+[\w/]~i', '$0', $statut);
But if the goal of this replacement is to keep all urls and transform them into links, you must change the pattern a little. And, why not, test them with filter_validate_url