auto correction last name with double name - php

I have a html form, where the user can write his / her own last name and send it to an php file, which checks if all is correct.
Now I would like to make an auto correction, for this case:
Last name: maX-poWEer
this should be corrected as follows
Max-Power
the first letters should be allways uppercase, and the others should be lowercase:
strtolower("maX-poWEer");
New Result:
max-power
But how can I realize the first letters of each name as uppercases?
ucwords() doenst work in this case

Use the mb_convert_case() function, this function can handle utf-8 characters, which is useful when dealing with international names.
$name = "maX-poWEer";
$formatted_name = mb_convert_case($name, MB_CASE_TITLE, "UTF-8");
echo $formatted_name; // Output: "Max-Poweer"

Related

special character renaming in php to underscores

What's a good way to rename all special characters collected in a form to be replaced with an underscore?
Here's an example of the special characters to be replaced:
[.!,;:##$%^&*|()?/\\\<>] space tab CR NL
I am tying into a piece of software that is downloaded by a user, and in that software, the renaming convention is to replace any of the characters listed above with an underscore. And for my web based application to work properly, it needs to collect some information in a form field that is named exactly as the user put it in the software, and have the naming convention work right. So when the mp3 file gets uploaded, and the ajax call checks for a file, it will match exactly, thus tripping off the rest of the functionality.
You're almost there already:
$str = "Zebo's [Test]";
echo preg_replace("~['.!,;:##$%^&*|()?/\\<> \t\r\n\[\]]~", "_", $str);
Output: Zebo_s__Test_
Edited to include [, ], and ' properly - didn't realize you meant that you wanted to replace those.
By the way... You say you want to replace "all special characters," and that your list above is just an "example." You may want to do something broader, like this:
preg_replace("~[^A-Za-z0-9]~", "_", $str);
This would also catch characters like the backtick and other special characters, such as:
`îõ§¶þäô
You can use:
$repl = preg_replace('~[.!,;:##$%^&*|()?/\\\<>]~', '_', $str);

preg_match doesn't accept new lines

I know it sounds a little strange, but I'll try to explain.
use this code to check if $text contains unsupported characters:
if(!preg_match('/\A[\w .,]+\z/', $text))
{
echo "Text contains unsupported characters.";
}
Now the problem is that $text is the text entered by the user in textarea.
$text_c = $_POST['text'];
$text = mysql_real_escape_string($text_c);
echo "<form method='post'><textarea name='text'>".$text."</textarea><br /><input type='submit' name='submit' value='Submit'></form>";
Everything works perfectly, except one thing. Example, if this is text entered in textarea:
Hello, my name is John.
OK.
Hello, my name is John.
I am 20 years old.
NO. Text contains unsupported characters. (refers to the new line, because content of the textarea change and have something like: My name is John.\r\nI am 20 years old.)
My question is: what I need to change to support new lines? I hope you understand.
EDIT: I just discovered, without mysql_real_escape_string everything works exactly as I want. Is there any solution to "combine" mysql_real_escape_string with preg_match (in my case) or mysql_real_escape_string supposed to drop?
You can use \s to represent whitespace (new lines, tabs and spaces).
So your regex becomes: /\A[\w .,\s]+\z/
Note \n \r are both valid escape characters and \r\n together will match a dos line break if you don't want to include spaces/tabs.
Good reference: http://www.regular-expressions.info/reference.html
It's not the same error; rather, it's not erroring for the same reason you think it is. It's not the newlines that are tripping your regex up, it's the numbers. ;-)
Add \d to your regex as well: /\A[\w\d\s.,]+\z/

PHP regex ampersand issue

I use the following regex to validate a username (input type text in a registration form) in order to make sure that the username contains ONLY alphanumeric characters, dot, dash or underscore.
if (!preg_match('/^[a-zA-Z0-9\.\_-]+$/',$my_name)) { echo 'no_valid'; }
When I type in the text field for instance % or # or # I get back correctly the error message that it's not a valid username, also the valid characters (.-_) are accepted, so it seems to work fine until the time I type & or +, then I can type any invalid character that I have already exclude before by using the preg_match.
Could anyone tell me why is this happening and how can I overcome this issue?
Problem is somewhere else. Your expression is correct. I tested with PHP. Since it happens with '&' character my guess would be that your data is not converted to URL safe characters before send. Try using encodeURI() function in JS.
if (!preg_match('/^[a-zA-Z0-9\.\_-]+$/',urldecode($my_name))) { echo 'no_valid'; }

PHP doesn't detect white space in string

I'm working on transferring data from one database to another. For this I have to map some values (string) to integers and this is where I run into a strange problem.
The string looks like this $string = "word anotherword"; so two words (or one space).
When I explode the string or count the amount of spaces it misses the white space. Why? I var_dumped the variable and it says it's a string.
Below is the code i'm using.
echo "<strong>Phases</strong>: ".$fases = mapPhase($lijst[DB_PREFIX.'projectPhase']);
The string that's being send to the function is for example "Design Concept". This calls the following function (where the spaces get ignored)
function mapPhase($phases){
echo "Whitespace amount: ".substr_count($phases, ' ')."<br />";
}
For the example string given this function echoes 0. What's causing this and how can i fix it? The strangest thing is that for one instance the function worked perfectly.
More than one whitespaces (in HTML) are always converter into one whitespace. For example code indents.
If you want to print more than one, one by one use &nbps; instead.
function mapPhase($phases){
echo 'Whitespace amount: '.substr_count($phases, ' ').'<br />';
}
It may well be that the alleged space in the string may not be a space as in ' ', but something similar, which gets rendered in the browser in the same way as ' ' would. (for a rudimentary list of possible characters: http://php.net/manual/en/function.trim.php)
Thus, checking what the whitespace exactly is may be the solution to that problem.
Maybe they are not even spaces. Try ord() for each symbol in your string.
ord(' ') is 32.
You can use:
$string = preg_replace('/\s+/', '', $string);

How to Convert Arabic Characters to Unicode Using PHP

I want to to know how can I convert a word into unicode exactly like:
http://www.arabunic.free.fr/
can anyone know how to do that using PHP considering that Arabic text may contains ligatures?
thanks
Edit
I'm not sure what is that "unicode" but I need to have the Arabic Character in it's equivalent machine number considering that arabic characters have different contextual forms depending on their position - see here:
http://en.wikipedia.org/wiki/Arabic_alphabet#Table_of_basic_letters
the same character in different position:
ب‎ | ـب‎ | ـبـ‎ | بـ‎
I think it must be a way to convert each Arabic character into it's equivalent number, but how?
Edit
I still believe there's a way to convert each character to it's form depending on positions
any idea is appreciated..
All what you need is function called: utf8Glyphs which you can find it in ArGlyphs.class.php download it from ar-php
and visit Ar-PHP for the ArPHP more information about the project and classes.
This will reverse the word with same of its characters (glyphs).
Example of usage:
<?php
include('Arabic.php');
$Arabic = new Arabic('ArGlyphs');
$text = 'بسم الله الرحمن الرحيم';
$text = $Arabic->utf8Glyphs($text);
echo $text;
?>
i assume you wnat to convert بهروز to \u0628\u0647\u0631\u0648\u0632 take a look at http://hsivonen.iki.fi/php-utf8/ all you have to do after calling unicodeToUtf8('بهروز') is to convert integers you got in array to hex & make sure they have 4digigts & prefix em with \u & you're done. also you can get same using json_encode
json_encode('بهروز') // returns "\u0628\u0647\u0631\u0648\u0632"
EDIT:
seems you want to get character codes of بب which first one differs from second one, all you have to do is applying bidi algorithm on your text using fribidi_log2vis then getting character code by one of ways i said before.
here's example:
$string = 'بب'; // \u0628\u0628
$bidiString = fribidi_log2vis($string, FRIBIDI_LTR, FRIBIDI_CHARSET_UTF8);
json_encode($bidiString); // \ufe90\ufe91
EDIT:
i just remembered that tcpdf has bidi algorithm which implemented using pure php so if you can not get fribidi extension of php to work, you can use tcpdf (utf8Bidi by default is protected so you need to make it public)
require_once('utf8.inc'); // http://hsivonen.iki.fi/php-utf8/
require_once('tcpdf.php'); // http://www.tcpdf.org/
$t = new TCPDF();
$text = 'بب';
$t->utf8Bidi(utf8ToUnicode($text)); // will return an array like array(0 => 65168, 1 => 65169)
Just set the element containing the arabic text to "rtl" (right to left), then input correctly spelled arabic and the text will flow with all ligatures looked for.
div {
direction:rtl;
}
On a side note, don't forget to read "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)"
Think about that : The "ba" (ب) arabic letter is a "ba" no matter where it appears in the sentence.
Try this:
<?php
$string = 'a';
$expanded = iconv('UTF-8', 'UTF-32', $string);
$arr = unpack('L*', $expanded);
print_r($arr);
?>
I'm totally agree with FloatBird about the use of the arabic.php which you will find it as he said at ar-php, The thing is they have changed the class name after version 4 from Arabic to I18N_Arabic so in order for the code to work using arabic.php ver 4.0 you need to change the code to
<?php
include('Arabic.php');
$Arabic = new I18N_Arabic('ArGlyphs');
$text = 'بسم الله الرحمن الرحيم';
$text = $Arabic->utf8Glyphs($text);
echo $text;
?>
Also notice that you need to put the php code file inside the I18N folder.
Anyway it is working fantastically, Thanks again FloatBird
I had a similar problem when I wanted to store an object that had values in Arabic, so writing in Arabic was stored as UNICODE," so the solution was as follows.
$detailsLog = $product->only(['name', 'unit', 'quantity']);
$detailsLog = json_encode($detailsLog, JSON_UNESCAPED_UNICODE);
$log->details = $detailsLog;
$log->save();
When you put the second parameter of the json_encode JSON_UNESCAPED_UNICODE follower, the Arabic words return without encoding.
i think you could try:
<meta charset="utf-8" />
if this does not work use FloatBird Answer

Categories