Can I store a special character inside a variable?
I mean something like:
<?php
$variable=' '
Then can i use something like echo $variable?
Yes, but don't take my word for it:
#!/path/to/php
<?
$variable = ' ';
echo $variable;
?>
Related
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.
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>';
?>
I need to get content from a file so that the escape sequences (like \n) got recognized as special characters.
Consider the code:
<?php
$f = file_get_contents("test.txt");
echo "$f";
?>
while test.txt contains only:
Test\nOnly
It echoes:
Test\nOnly
while I'd like to have:
Test
Only
Is there a way to accomplish it with file_get_content or should I use something else (like output buffering)?
try using printf which the function outputs a formatted string.
<?php
$f = file_get_contents("test.txt");
printf ($f);
?>
In php, If I have
$ABC = "love";
echo $abc;
-----
Display: love
That's so okay. BUT if I have
$ABC = "$love"; //Have $ in the string.
echo $abc;
-----
Display: Unkown variable love
I want:
$ABC = "$love"; //Have $ in the string.
echo $abc;
-----
Display: $love //$love are all text character
How can I change $ become character, It will not be $ as the variable. Please help me.
Use \ to escape it, or just use single quote.
$ABC = "\$love";
// or
$ABC = '$love';
<?php
$la='$love';
echo $la;
echo '<br>';
$lb="\$lover";
echo $lb;
?>
outputs
$love
$lover
Learn the differences between single and double quotes when defining a string.!
I want to pass the php variable value in onClick function.
When i pass the php variable, in the UI i am getting the variable itself instead I need the value in the variable.
Below is code snippet, please help me.
<?php
print '<td>';
$node = $name->item(0)->nodeValue;
$insert= "cubicle"."$node<br>";
Echo '<a href= "#" onClick= showDetails("$node");>'. $insert .'</a> ';
print '</td>';
?>
Variable parsing is only done in double quoted strings. You can use string concatenation or, what I find more readable, printf [docs]:
printf('%s ', $node, $insert);
The best way would be to not echo HTML at all, but to embed PHP in HTML:
<?php
$node = $name->item(0)->nodeValue;
$insert = "cubicle" . $node;
?>
<td>
<a href= "#" onClick="showDetails('<?php echo $node;?>');">
<?php echo $insert; ?> <br />
</a>
</td>
You have to think less about quotes and debugging your HTML is easier too.
Note: If $node is representing a number, you don't need quotations marks around the argument.
you shouldn't be wrapping $node in '"':
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
If you want the value of $node to be in a string, thn i would do:
Echo '<a href= "#" onClick= showDetails("' . $node. '");>'. $insert .'</a> ';
$var = "Hello World!";
echo "$var"; // echoes Hello World!
echo '$var'; // echoes $var
Don't mix up " and ', they both have importance. If you use some " in your string and don't want to use the same character as delimiter, use this trick:
echo 'I say "Hello" to ' . $name . '!';
I think you are searching for PHP function json_encode which converts PHP variable into JavaScript object.
It's more secure than passing the value right in the output.
Echo '<a href= "#" onClick= showDetails("'.$node.'");>'. $insert .'</a> ';
I have been using curly braces lately instead of concatenation. I think it looks better/is more readable, and mostly I find it is easier and less prone to human error - keeping all those quotes straight! You will also need quotes around the contents inside of onClick.
Instead of this:
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
Try this:
Echo '{$insert} ';
As a side note, I usually use double quotes to wrap my echo statement and strictly use single quotes within it. That is just my style though. However you do it, be sure to keep it straight. So my version would look like this:
echo "<a href='#' onClick='showDetails({$node});'>{$insert}</a>";