Preg_match with fullwidth numbers [duplicate] - php

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?)

Related

How to prevent PHP from renaming '&times' to 'x' symbol? [duplicate]

This question already has answers here:
Why is "&reg" being rendered as "®" without the bounding semicolon
(8 answers)
Should an ampersand be URL encoded in a query string?
(2 answers)
Closed 2 years ago.
I have with an unwanted replaced, and not able to figure out how to fix it.
When you echo the following string in PHP
echo('?hash=123&rid=111&timestamp=123');
The output is:
?hash=123&rid=111×tamp=123
Note that &timestamp has been replaced with ×tamp
I tried to escape it by using \&timestamp but that doens't work.
How can I prevent PHP replacing this?
You can reproduce this error online using http://phptester.net/
You have to escape that string, because & is a special symbol in HTML.
echo htmlspecialchars('?hash=123&rid=111&timestamp=123');
More information on the PHP site: https://www.php.net/manual/en/function.htmlspecialchars.php

How to find a character using index in PHP string? [duplicate]

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];

Can someone help me with my Regex to get the text in html code? [duplicate]

This question already has answers here:
Regex select all text between tags
(23 answers)
Closed 8 years ago.
I need get the text from text in the html code using regex in php code. But maybe it's seem I wrong somewhere in my code. Please can you help me fix my code. Thank you very much!
This is my regex pattern:
/<a\shref="\/vn\/tags\/.*">(?P<tags>.*)<\/a>/
And this is example subject:
<ul class="clearfix"><li><span class="tagBoxTitle">Từ khóa: </span></li><li>Thực phẩm, </li><li>giá-cả, </li><li>hàng-tiêu-dùng, </li><li>giảm-giá, </li><li>cước-vận-tải, </li><li>giá-xăng, </li><li>xăng-dầu, </li><li>hàng-hóa, </li><li>CPI, </li><li>Tết-nguyên-đán</li></ul>
Hope I can get an answer as soon as possible, thanks again!
Don't parse html with regex.
If you wanna regex solution then turn all the .* to .*? in-order to do a non-greedy match.
<a\shref="\/vn\/tags\/.*?">(?P<tags>.*?)<\/a>
DEMO

Need to replace deprecated ereg_replace [duplicate]

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

HTML foreign language characters [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular Expression To Anglicize String Characters?
What would be the best way to convert foreign language characters to english ones? For example ü to u.
There are only a couple of reasons to do this (url friendliness, mostly). You want strtr.
It basically works like this:
$addr = strtr($addr, "äåö", "aao");
The 2nd comment in the manual has a nice translation table for you.
$text = mb_str_replace('ü','u', $text);
To find all non English character using:
preg_match('#[^a-z0-9\-\.\,\:\;]#', $text, $characters);

Categories