简体   繁体   中英

strip Unicode? hyphen and whitespace from string php

i am trying to grab a label value from a select list using jquery, write it to a hidden text input and then display that value in php template. --(there is no way to access the field directly in the data, this is my work around.)

so i can grab the label value, pass it to the hidden and display. BUT the label is formatted like so " - latest news" i want to strip the whitespace and hyphen/minus symbol so i am left with "latest news"

by trying the usual functions and them not working it occurred to me, when it didnt work that these may be unicode characters. when i use the following:

$mycategory = preg_replace('/^\p{Z}+|\p{Z}+$/u', '', $mycategory);
$mycategory = htmlentities($mycategory, ENT_QUOTES, "UTF-8");
$mycategory = str_replace("-", "", $mycategory);

echo '<h1>'.$mycategory.'</h1>';

line 1 i replaced the unicode white space at the front. that returns "- latest news" line 2: i changed the entity into html line 3: i got rid of the entity. but that returns " latest news"

so i cant get rid of that last space with trim or the preg_replace. i have no idea how to get rid of it. if i put the blankspace in with the search term on line three it breaks the replace.

Well,,, grrr..had to open php :)

 $str = '- Latest news !*#';
 echo preg_replace('/[^[:alnum:]\s]/','',$str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM