I need to remove # from string. I found this method:
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
It doesn't work for the Thai language. I want to remove like this:
from
#Apple #ผลไม้
to
#Apple #ผลไม้
I can not understand why str_replace() did not work for you. This will do the job:
function cleanString($string) {
$search = array('', '', '');
$replace = array('', '', '');
return str_replace($search, $replace, $string);
}
$string = '#Apple #ผลไม้';
echo $string . "\n";
echo cleanString($string) . "\n";
Output is:
#Apple #ผลไม้
#Apple #ผลไม้
Working example can be found at http://sandbox.onlinephpfunctions.com/code/bbdbdf0758e5ea06faf32281021ae859b6d75a51
Related
I am trying to include a php function within a string but my syntax is wrong. Could anyone help please?
$string = "<div id=\"apostnav\"><span class=\"calcir b-" . $string=get_field('br_category');
$string=preg_replace("/[^a-zA-Z]/", "", $string);
echo strtolower($string) . "></span></div>";
Try this:
$string=get_field('br_category');
$string=preg_replace("/[^a-zA-Z]/", "", $string);
$string = "<div id=\"apostnav\"><span class=\"calcir b-" . strtolower($string) . "\"></span></div>";
I moved up two lines of code andn removed an echo from the last one.
Here the code with few optimizations:
$string = get_field('br_category');
$string = preg_replace("/[^a-zA-Z]/", '', $string);
$stringLower = strtolower($string);
$string = "<div id='apostnav'><span class='calcir b-$stringLower'></span></div>";
You can use printf too in order to print your html string with the variable:
printf('<div id="apostnav"><span class="calcir b-%s"></span></div>', strtolower(preg_replace('/[^a-zA-Z]/', '', get_field('br_category'))));
$cont=htmlspecialchars(file_get_contents("https://myanimelist.net/anime/30276/One_Punch_Man"));
function getBetween($string, $start = "", $end = ""){
if (strpos($string, $start)) { // required if $start not exist in $string
$startCharCount = strpos($string, $start) + strlen($start);
$firstSubStr = substr($string, $startCharCount, strlen($string));
$endCharCount = strpos($firstSubStr, $end);
if ($endCharCount == 0) {
$endCharCount = strlen($firstSubStr);
}
return substr($firstSubStr, 0, $endCharCount);
} else {
return '';
}
}
$name=getBetween($cont,'title',' - MyAnimeList.net');
//$name=preg_replace('/[^a-zA-Z0-9 \p{L}]/m', '', $name);
preg_replace('/(*UTF8)[\>\<]/m', '', $name);
trim($name," ");
//$name=str_replace("gt", "", $name);
echo $name;
i want to find the text between title tags. how to do this?
for example in this page title contains 'One Punch Man - MyAnimeList.net' i want to get that
Just use string replace function:
$string = '<BoomBox>';
$string = str_replace('<', '', $string);
$string = str_replace('>', '', $string);
echo $string; // output: Boombox
http://php.net/manual/en/function.str-replace.php
You edited your answer, and we can now see you are dealing with XML/HTML. It's always better to work with the DOM classes. Never use regex! There is a famous Stack Overflow post explaining why never to parse html with regex. Try this solution instead:
<?php
$dom = new DOMDocument();
$dom->loadHTML('<title>BoomBox</title>');
echo $dom->getElementsByTagName('title')->item(0)->textContent;
http://php.net/manual/en/class.domdocument.php
http://php.net/manual/en/class.domnode.php
See it working here https://3v4l.org/EjPQd
You can use preg_replace();, or strip_tags();.
Example preg_replace();:
$str = '> One Punch Man';
$new = preg_replace('/[^a-zA-Z0-9 \p{L}]/m', '', $str);
echo $new;
Output: One Punch Man
Above example will only allow a-z, A-Z and 0-9. You can expand this.
Example strip_tags();:
$str = '<title> BoomBox </title>';
$another = strip_tags($str);
echo $another;
Output: BoomBox
Documentation:
http://php.net/manual/en/function.preg-replace.php // preg_replace();
http://php.net/manual/en/function.strip-tags.php // strip_tags();
You can also use a single call to str_replace with the ['<','>'] as the search argument:
$string = '<BoomBox>';
echo str_replace(['<', '>'], '', $string) . PHP_EOL;
// => Boombox
Or, you may use a regex with preg_replace (especially, if you plan on adding more restrictions for in-context matching to it):
echo preg_replace('~[<>]~', '', $string);
// => Boombox
See the PHP demo.
I have clean function for remove special caracter from string but that function also removing Turkish caracter (ı,ğ,ş,ç,ö) from string
function clean($string) {
$string = str_replace(' ', ' ', $string);
$string = preg_replace('/[^A-Za-z0-9\-]/', ' ', $string);
return preg_replace('/-+/', '-', $string);
}
How can I fix it ?
Add those characters you want to keep to preg, also add Upper cases if neededç I edited your code:
function clean($string) {
$string = str_replace(' ', ' ', $string);
$string = preg_replace('/[^A-Za-z0-9\-ığşçöüÖÇŞİıĞ]/', ' ', $string);
return preg_replace('/-+/', '-', $string);
}
Test:
$str='Merhaba=Türkiye 12345 çok çalış another one ! *, !#_';
var_dump(clean($str));
//Output: string(57) "Merhaba Türkiye 12345 çok çalış another one "
You can use iconv to replacing special characters like à->a, è->e
<?php
$string = "ʿABBĀSĀBĀD";
echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $string);
// output: [nothing, and you get a notice]
echo iconv('UTF-8', 'ISO-8859-1//IGNORE', $string);
// output: ABBSBD
echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
// output: ABBASABAD
// Yay! That's what I wanted!
?>
Credits:
https://gist.github.com/swas/10643194
#dmp y
#Nisse Engström
Maybe you can try:
function clean($string) {
$string = str_replace(' ', ' ', $string);
$string = preg_replace('/[^A-Za-z0-9ĞİŞığşçö\-]/', ' ', $string);
return preg_replace('/-+/', '-', $string);
}
Which special characters you want to replace?
Maybe be it'll be easier to change a paradigm of cleaning from everything except ... to something concrete.
<?php
function garbagereplace($string) {
$garbagearray = array('#','#','$','%','^','&','*');
$garbagecount = count($garbagearray);
for ($i=0; $i<$garbagecount; $i++) {
$string = str_replace($garbagearray[$i], '-', $string);
}
return $string;
}
echo garbagereplace('text##$text%^&*text');
?>
I have a webpage which includes a hyperlink as follows:
$name = "Hello World";
echo "<a href='page.php?name='". preg_replace(" ", "_", $name) "'> bla bla </a>"
This generates the following link successfully:
...page.php?name=Hello_World
in my page.php I try to reverse the operation:
if($_SERVER[REQUEST_METHOD] == "GET"){
$name = $_GET['name'];
//testing if the problem is with GET
echo $name;
//now here's the problem:
$string = preg_replace("_", " ", $name);
echo $string;
}
the $name echoes correctly but the $string is always null
I've tried all possible combinations like ~~ and // and [_] and \s and using $_GET directly like:
preg_replace("_", " ", $_GET['name']);
none of them worked.
This problem has burned most of my day.
Any help is appreciated.
preg_replace accepts a regular expression as it's first arguments. Neither " " nor "_" are valid regular expressions.
In this case you can use str_replace.
The preg_replace syntax is incorrect as pointed by #Halcyon, the following is correct:
$string = preg_replace('/_/', ' ', $name);
But for such a simple find/replace you can use str_replace instead:
$string = str_replace("_", " ", $name);
echo $string;
you can create a function like sefurl
function makesefurl($url=''){
$s=trim($url);
$tr = array('ş','Ş','ı','I','İ','ğ','Ğ','ü','Ü','ö','Ö','Ç','ç','(',')','/',':',',');
$eng = array('s','s','i','i','i','g','g','u','u','o','o','c','c','','','-','-','');
$s = str_replace($tr,$eng,$s);
$s = preg_replace('/&amp;amp;amp;amp;amp;amp;amp;.+?;/', '', $s);
$s = preg_replace('/\s+/', '-', $s);
$s = preg_replace('|-+|', '-', $s);
$s = preg_replace('/#/', '', $s);
$s = str_replace('.', '.', $s);
$s = trim($s, '-');
$s = htmlspecialchars(strip_tags(urldecode(addslashes(stripslashes(stripslashes(trim(htmlspecialchars_decode($s))))))));
}
echo "<a href='page.php?name='". makesefurl($name) "'> bla bla </a>";
and than you can convert it to before makesefurl function all you need to create another functin like decoder or encoder html command
how to replace <p>hello</p> <p>world</p> to hello<br />world <br />
I've tried searching on stack but there is no matched result.
You could do this by using str_replace() function.
For instance:
$string = "<p>hello</p> <p>world</p>";
$string = str_replace('<p>', '', $string);
$string = str_replace('</p>', '<br />' , $string);
I try this myself and get what I expected
$pattern = '/<p>(\w+)\<\/p>/';
$subject = '<p>hello</p><p>world</p>';
$replacement = '${1}<br/>';
$out = preg_replace($pattern, $replacement, $subject);
I just wonder which is better regex or str_replace
I wrote a better solution, hope everybody can see it helpful and maybe improve it
$pattern = '/<p(.*?)>((.*?)+)\<\/p>/';
$replacement = '${2}<br/>';
$subject = 'html string';
$out = preg_replace($pattern, $replacement, $subject);
Use this to prevent breaking the first and last <p></p> :
$string = str_replace($string, '</p><p>', '');
But if a space comes between the tags, it won't work.
<?php
$str = '<p>hello</p> <p>world</p>';
$replaceArr = array('<p>', '</p>', '</p> <p>');
$replacementArr = array('', '', '<br />');
$str = str_replace($replaceArr, $replacementArr, $str);
echo $str;
?>
Try above code.