I have run into a strange problem. I am trying to trim a string using php trim(), but it is not working.
Here is my code
echo $deal->{self::DEAL_BUSINESS};
echo '<br>';
echo trim($deal->{self::DEAL_BUSINESS});
echo '<br>';
And here is the output
Alkaram Studio
Alkaram Studio
If it is not clear from the output. There is a space in the beginning of both untrimmed and the trimmed string.
In the view source I got this.
Alkaram Studio
Try the following:
echo trim($deal->{self::DEAL_BUSINESS}, "\xC2\xA0\n");
Or
$text = preg_replace('~\x{00a0}~siu','',$deal->{self::DEAL_BUSINESS});
echo $text;
Related
I am adding a pad to my string, to fill with spaces, but it doesn't work
the code is here
<?php
$string1 = "Product 1 ";
$newString = str_pad($string1,100);
echo $newString."test";
echo "<br>";
$string2 = "Product 2222 ";
echo str_pad($string2,100," ")."test";
echo "<br>";
?>
the output is like this:
Product 1 test
Product 2222 test
You could try $str = str_pad($string2,(100*strlen(" "))," ")."test"; instead.
renders to a non-breaking-space in html (and when writing to document with fpdf).
Please note this can only work with fpdf when you tell it to write all lines as html! And the encoding should be utf-8 probably
$fpdf->Write(iconv('UTF-8', 'windows-1252', html_entity_decode($str)));
When the output of the PHP is converted to HTML, all the white spaces except the first are removed and it is the default feature of HTML and web browsers. so the output will not be correct.
You have to use the " " instead of white space in the str_pad function. HTML don't ignore the " " and against each existance of it, HTML adds a white space to the string.
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 am having a problem with capturing text when I make an exec call to a perl script that just prints a lot of text. What happens when I run the following code is I get a 1 word result: "Array". I need to be able to capture the results so that I can change them around just a little bit. Here is the code:
<?php
$lastline = exec("perl parseOutput.pl",$retVal);
echo $retVal;
?>
How do I work around this?
You have an array of the lines of text that was outputted.
Do something like this:
echo implode( "\n", $retVal);
Or
echo implode( "<br />\n", $retVal);
And you'll see all of the output generated by the perl script.
just use shell_exec()
$fullResult = shell_exec("perl parseOutput.pl");
echo $fullResult;
i have a lil problem here..i'm using str_replace to replace the most common words..and for some reason its replacing every letter except caps.
for example..if i had the code below
$str ="Fat string of Text.";
$commonwords = array('fat','of','random');
$cleantext = str_replace($commonwords,'',$str);
echo $cleantext;
it would echo.. F T
any ideas what i did wrong..
thanks in advance
and oh..i tried str_ireplace.. but nothing
This echos "Fat string Text".
Your PHP installation may be wrong or your posted code that does not exactly match the program you are running
Also, str_ireplace echos "string Text".
Can't reproduce that on PHP 5.3.3. I get:
php > $str ="Fat string of Text.";
php > $commonwords = array('fat','of','random');
php > $cleantext = str_replace($commonwords,'',$str);
php > echo $cleantext;
Fat string Text.
php > $cleantext = str_ireplace($commonwords,'',$str);
php > echo $cleantext;
string Text.
as expected.
I have a string with XML:
$string =
"
<shoes>
<shoe>
<shouename>Shoue</shouename>
</shoe>
</shoes>
";
And would like display it on my website like this:
This is XML string content:
<shoes>
<shoe>
<shouename>Shoue</shouename>
</shoe>
</shoes>
So I would like to do it:
on site, not in textbox
without external libraries, frameworks etc.
formatted with proper new lines
formatted with tabs
without colors etc., only text
So how to do it in plain and simple way?
If you just want a plain-text representation of your (pre-formatted) string, you can wrap it in HTML <pre/> tags and use htmlentities to escape the angle brackets:
<?PHP echo '<pre>', htmlentities($string), '</pre>'; ?>
you can use htmlentities(), htmlspecialchars() or some similar function.
It should work like that:
echo '<p>This is XML string content:</p>'
echo '<pre>';
echo htmlspecialchars($string);
echo '</pre>';
If it is a SimpleXMLobject
<pre>
<?php
echo htmlspecialchars(print_r($obj,true));
?>
</pre>
I searched for a solution to have slightly coloured output:
$escaped = htmlentities($content);
$formatted = str_replace('<', '<span style="color:blue"><', $escaped);
$formatted = str_replace('>', '></span>', $formatted);
echo "<pre>$formatted</pre>\n";