HTML foreign language characters [duplicate] - php

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

Related

Preg_match with fullwidth numbers [duplicate]

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

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

How to check in php if string contains special characters like { and } [duplicate]

This question already has answers here:
preg_match special characters
(7 answers)
Closed 5 years ago.
There is this input field where I want that user ISN'T able to use following special marks: {}[]$
Right now I have following solution in my code but problem is that it isn't allowing ä, ö, ü or other characters like that.
if (preg_match('/^[-a-zA-Z0-9 .]+$/', $string) || empty($string)){
echo "Everything ok!";
else{
echo "Everything not ok!";}
Because of that, I tried using preg_match('/^[\p{L}\p{N} .-]+$/', $string) because it was said to allow characters from any language but that solution isn't allowing marks like # and *, which I think may be needed. So any solution which would allow anything except {}[]$ -marks? Any help is much appreciated since I can't figure out what to write to get this working.
this is how i do it :
if (preg_match('/[^a-zA-Z\d]/', $string)) {
//$string contains special characters, do something.
}

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

Regex not playing well with Russian text [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
regexp with russian lang
I have a regular expression that filters out certain links out of a text and attaches a file icon based on the filetype of the link. Like this:
$text = preg_replace('((<a href="[\w\./:]+getfile.php\?id='.$file.'"([a-zA-Z0-9_\- ,\.:;"=]*)>)([a-zA-Z0-9_,\.:;&\-\(\)\<\>\'/ ]+)</a>)','\\1'.fileicon($name).'</a> \\1\\3</a> ('.($pagecount?$pagecount." ".($pagecount>1?$pages:$page1).", ":"").readable_filesize($size,1).')',$text);
this worked great until I tried this with some russian text. The input would be something like:
Русский
But it won't show the icon before the link and file information after the link, making me suspect the regex doesn't play well with Russian text. What could be the case here?
You shall use u modifier when working with Unicode strings:
preg_replace('/>([^<]+)</u', '', $string);
Your character class only allows [a-zA-Z0-9_,\.:;&\-\(\)\<\>\'/ ]. There are no russion characters in there.
You can fix this by adding the relevant characters to the class. If you only need to support russian, \p{InCyrillic} should do it. If you want all unicode letters, \p{Letter}.
You can simplify your regexp down to something like
$re = "~
(<a\s+href=\".+?getfile\.php\?id=$file\".*?>)
(.+?)
</a>
~xui";
this should solve the Cyrillic problem automatically.
Cyrillic unicode characters are within the range \x0400-\x04FF. Add this range in your character class.

Categories