Trim() not work in PHP7 - php

I wrote a short test code in PHP7:
<?php
$str1=' bigapple ';
echo strlen($str1);
trim($str1) ;//or trim($str1," ")
echo strlen($str1);
?>
But whenever I use trim on $str1 ,the strlen would be return 10.
Can someone tell me the reason why? I've been searching it but find nothing.

You need to store trim string to any variable or you need to print trim string as following:
echo strlen(trim($str1));

You have not assigned the trimmed value in another variable. Try as below :
<?php
$str1=' bigapple ';
echo strlen($str1).'<br>';
$str2 = trim($str1);
echo strlen($str2).'<br>';
?>

Related

Error getting part of url separated by blackslash

I want to get last part of url separated by blackslash
Here is my code
<?php
$str = "C:\xampp\htdocs\sanimailer\storage\mail-attachments\10819_21098672691424384960_C
opy_of_tristan_data_cleanup_version_FBL_3.2.xlsx";
echo basename($str);
?>
I want output as this
10819_21098672691424384960_Copy_of_tristan_data_cleanup_version_FBL_3.2.xlsx
But I am getting output as this
Not sure why I am getting such output .
You need to convert " to ' and then it will work:-
<?php
$str = 'C:\xampp\htdocs\sanimailer\storage\mail-attachments\10819_21098672691424384960_C
opy_of_tristan_data_cleanup_version_FBL_3.2.xlsx';
echo basename($str);
?>
Output:- https://eval.in/829073
The problem you are facing because "\10" converted to that symbol
check here:- https://eval.in/829077

str_replace with htmlspecialchars return empty result

I want to replace all special character (in array), I used htmlspecialchars, but it does not work I found empty result !!
this is my instruction:
str_replace( array('è','é','ê','ë'),
array('e','e','e','e'),
htmlspecialchars(strtolower("Elément")) );
thank's for helping...
Short answer: you must use mb_strtolower instead strtolower,
run the snippet below you will find why:
<?php
$a = str_replace( array('è','é','ê','ë'), array('e','e','e','e'), htmlspecialchars(strtolower("Elément")) );
echo "\n0.".$a;
echo "\n1.".htmlspecialchars(strtolower("Elément"));
echo "\n2.".strtolower("Elément");
echo "\n3.".mb_strtolower("Elément");
echo "\n4.".htmlspecialchars(mb_strtolower("Elément"));
$a = str_replace( array('è','é','ê','ë'), array('e','e','e','e'), htmlspecialchars(mb_strtolower("Elément")) );
echo "\n5.".$a;
see also enter link description here
You could use a slugger, such as https://packagist.org/packages/javiereguiluz/easyslugger

Deleting spaces in string and put one in front

High,
I'm using this function
function special_chars_replace($string){
$result = preg_replace("/[&%\$##'\*:\/\"\[\]\{\};\(\)\|\\\=!\^\?`~.,\+-]/", "", $string);
return $result;
}
to delete all spaces in a css class name.
<?php echo special_chars_replace(strtolower(str_replace(" ","",$itemTags))); ?>
How do i preserve the first space before the name? So i can use it for a css class name. For example: class="tags tag01 tag02"
Just add the space before you echo the string:
<?php
echo " ".special_chars_replace(strtolower(str_replace(" ","",$itemTags)));
?>
You can use this
<?php echo implode(" ",explode(" ",$itemTags)); ?>
Regular expression is the most effective way to do this.
echo preg_replace(' +', ' ', $itemTags);
What this does is look for one or more spaces (that's what the + does), and replaces it with a single space.
Code typed from memory.

Text between less than and greater than not output PHP

i use (str_replace) function to replace ##ID## in youtube url with this regular expression : (?P<id>[a-z-A-Z_0-9]+)
so i use this code to do this :
<?php
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo $lastchange;
?>
i get the output in the browser like this : https://www.youtube.com/watch?v=(?P[a-z-A-Z_0-9]+), its looks like <id> not show up !
i try this simple code :
<?php
echo "This is my <id>";
?>
but i just get this is my in the browser !
What's the probleme ? and how i can fix it , thanks
is being interpreted as HTML so your browser is parsing it and since it is not a renderable element, it shows nothing. Try:
<?php
echo "This is my <id>
?>
As for the str_replace, it's doing exactly what the function is supposed to be doing. If you're looking to use regular expressions in string replacements, use preg_replace
The tag <id> is being removed by your browser. It is really there if you watch the source code. Maybe you should try:
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo urlencode( $lastchange );
Problem is with the line:
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
str_replace does not use regex.
You will need preg_replace
$pattern = '(<id>[a-z-A-Z_0-9]+)'
$replacement = '##ID##'
$string = $urlbase
$lastchange = preg_replace($pattern, $replacement, $string);
Also < and > are html entities which means they are reserved chars for HTML they have some special meanings if you want to show them then you must use there entity name eg < and > in your case respectively.
<?php
echo " echo "This is my <id>";
?>

Looking for some REGEX help in PHP

I have a string:
[COLOR=gray]A bunch of text.[/COLOR]
And I would like to write a preg_replace that removes everything between "[COLOR=gray]" and "[/COLOR]" -- if it's possible to remove those tags as well, that's great, otherwise I can do a simple replace afterward.
$str = 'dfgdfg[COLOR=gray]A bunch of text.[/COLOR]dfgdfgdfgfg';
$str1 = preg_replace('/\[COLOR=gray\].*\[\/COLOR\]/',"",$str);
echo $str1;
OR
if COLOR is not always gray
$str = 'dfgdfg[COLOR=gray]A bunch of text.[/COLOR]dfgdfgdfgfg';
$str1 = preg_replace('/\[COLOR=\w+\].*\[\/COLOR\]/',"",$str);
echo $str1;

Categories