This question already has answers here:
PHP new line break in emails
(8 answers)
Closed 7 years ago.
I have string
$st1="string 1"; $str2="string 2"; $str="string 3";
I need to make a above strings like this
$data='string 1
string 2
string 3';
i tried string concatenation
$data=$str1 . $str2 . $str3;
But I get output like this
$data = 'string 1 string 2 string 3;
any other method is there to get output like above?
Try to use as below :
$data=$str1 ."<br>". $str2 ."<br>". $str3;
$data = "{$str1}\n{$str2}\n{$str3}";
\n (in "") is a symbol of newline
try this
$data= $str1."<br/>".$str2."<br/>".$str3;
Use <br /> tag line break
$dta=$str1 . '<br />' . $str2. '<br />' . $str3;
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 1 year ago.
request
http://a.com/?q=\xC3\xA2\xB0\xED
source
$str1 = "\xC3\xA2\xB0\xED";
$str2 = '\xC3\xA2\xB0\xED';
$str3 = $_GET['q'];
echo "string1 : " . mb_detect_encoding($str1) . "<br>";
echo iconv("CP949", "UTF-8", $str1) . "<br>";
echo "string2 : " . mb_detect_encoding($str2) . "<br>";
echo iconv("CP949", "UTF-8", $str2) . "<br>";
echo "string3 : " . mb_detect_encoding($str3) . "<br>";
echo iconv("CP949", "UTF-8", $str3) . "<br>";
response
string1 : UTF-8
창고
string2 : ASCII
\xC3\xA2\xB0\xED
string3 : ASCII
\xC3\xA2\xB0\xED
$str2, $str3 decode is fail..
How can i fix it?
And Why is it different.. single quote string VS double quote string
Version : PHP 7.1.30
Thanks in advance
When you use single quote for a string each character is as it is and php doesn't interpret it, so $str2 can't convert to another encoding.
Also Query strings assumes as single quote strings so $str3 is like $str2.
And the solution is stripcslashes. it actually converts a single quote string to double quote string.
and you can fix it in this way:
$str2 = '\xC3\xA2\xB0\xED';
$str2 = stripcslashes($str2);
$str3 = $_REQUEST["q"];
$str3 = stripcslashes($str3);
I am working on scraping and then parsing an HTML string to get the two URL parameters inside the href. After scraping the element I need, $description, the full string ready for parsing is:
<a target="_blank" href="CoverSheet.aspx?ItemID=18833&MeetingID=773">Description</a><br>
Below I use the explode parameter to split the $description variable string based on the = delimiter. I then further explode based on the double quote delimiter.
Problem I need to solve: I want to only print the numbers for MeetingID parameter before the double quote, "773".
<?php
echo "Description is: " . htmlentities($description); // prints the string referenced above
$htarray = explode('=', $description); // explode the $description string which includes the link. ... then, find out where the MeetingID is located
echo $htarray[4] . "<br>"; // this will print the string which includes the meeting ID: "773">Description</a><br>"
$meetingID = $htarray[4];
echo "Meeting ID is " . substr($meetingID,0,3);
?>
The above echo statement using substr works to print the meeting ID, 773.
However, I want to make this bulletproof in the event MeetingID parameter exceeds 999, then we would need 4 characters. So that's why I want to delimit it by the double quotes, so it prints all numbers before the double quotes.
I try below to isolate all of the amount before the double quotes... but it isn't seeming to work correctly yet.
<?php
$htarray = explode('"', $meetingID); // split the $meetingID string based on the " delimiter
echo "Meeting ID0 is " . $meetingID[0] ; // this prints just the first number, 7
echo "Meeting ID1 is " . $meetingID[1] ; // this prints just the second number, 7
echo "Meeting ID2 is " . $meetingID[2] ; // this prints just the third number, 3
?>
Question, why is the array $meetingID[0] not printing the THREE numbers before the delimiter, ", but rather just printing a single number? If the explode function works properly, shouldn't it be splitting the string referenced above based on the double quotes, into just two elements? The string is
"773">Description</a><br>"
So I can't understand why when echoing after the explode with double quote delimiter, it's only printing one number at a time..
The reason you're getting the wrong response is because you're using the wrong variable.
$htarray = explode('"', $meetingID);
echo "Meeting ID0 is " . $meetingID[0] ; // this prints just the first number, 7
echo "Meeting ID1 is " . $meetingID[1] ; // this prints just the second number, 7
echo "Meeting ID2 is " . $meetingID[2] ; // this prints just the third number, 3
echo "Meeting ID is " . $htarray[0] ; // this prints 773
There's an easier way to do this though, using regular expressions:
$description = '<a target="_blank" href="CoverSheet.aspx?ItemID=18833&MeetingID=773">Description</a><br>';
$meetingID = "Not found";
if (preg_match('/MeetingID=([0-9]+)/', $description, $matches)) {
$meetingID = $matches[1];
}
echo "Meeting ID is " . $meetingID;
// this prints 773 or Not found if $description does not contain a (numeric) MeetingID value
There is a very easy way to do it:
Your Str:
$str ='<a target="_blank" href="CoverSheet.aspx?ItemID=18833&MeetingID=773">Description</a><br>';
Make substr:
$params = substr( $str, strpos( $str, 'ItemID'), strpos( $str, '">') - strpos( $str, 'ItemID') );
You will get substr like this :
ItemID=18833&MeetingID=773
Now do whatever you want to do!
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
I have the following string in PHP:
$string = "<img src=\"url\" >HELLO WORLD<ol>I must replace the text after img and before ol.</ol>";
print htmlentities($string);
I want to find the substring HELLO WORLD (or whatever substring is, that is just an example of a text that will be completely dynamical), using the delimiters : "<img ... >" and "<ol>" and add <h3> delimiters. So the string above would result in:
<img src="url" ><h3>HELLO WORLD</h3><ol>I must replace the text after img and before ol.</ol>
I have tried the following code, of course with no success:
$string = preg_replace("/\<img (.*?)\> (.*?)\<ol\>/", "<img (.*?)><h3> (.*?)</h3></ol>", $string);
I know how to make very easy substituions, but the above condition is very far from my understanding.
I have found the answer by trial-and-error:
$string = "<img src=\"url\" >HELLO WORLD<ol>I must find the text after img and before ol.</ol>";
$string2 = preg_replace("/<img (.*?)>(.*?)<ol>/", "<img $1><h3>$2</h3><ol>", $string);
print htmlentities($string) . " <br />" . htmlentities($string2);
Explanation: I add the delimiters, and use $1 and $2 for matching the results between the delimiters in the right order.
I'm making this chat server, but it doesn't work quite well. When you send a piece of text, it first gets encoded by the function base64_encode() and then gets sent to a MySQL database.
Then the receiver gets the text from that same MySQL database, which is of course first decoded by the function base64_decode().
The only problem is with the special characters like \n \' and \t: when I get the data from the database and print it between two textarea tags, I see \n as a string, and not as actual line breaks.
In short, I need to fix this problem:
$String = 'Line 1 \n Line 2';
print '<textarea>' . $String . '</textarea>';
//The result I want
//<textarea> Line 1
//Line 2 </textarea>
The function nl2br doesn't work, because tags inside a textarea tag won't work, and also because there other characters like apostrophes.
Could anybody help me?
Thanks!
You need to enclose your string into double quotes, for special characters to be evaluated.
$String = "Line 1 \n Line 2";
print '<textarea>' . $String . '</textarea>';
If you change this:
$String = 'Line 1 \n Line 2';
print '<textarea>' . $String . '</textarea>';
to this:
$String = "Line 1 \n Line 2"; // double quote
print '<textarea>' . $String . '</textarea>';
... you will get the output you want.
This one is also works same as using " ... ", however maybe helps in your case:
$string = <<<EOT
Line 1 \n Line 2
EOT;
echo '<textarea>' . $string . '</textarea>';
As the others said, your problem is Single-Quotes.
This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
I have a textbox named 'fname'. I need to echo out the input of this box inside double quotes on a another page.
User enters: Test123
returrns: "Test123"
so how can I do that with $_POST["fname"] ?
Or you can use:
echo "Hi how are you {$_POST['fname']} ? I am fine thanks";
If you want to use it in a string surrounded by letters, simply use curly brackets {}.
Put the $_POST['fname'] to curly brackets.
Try
<?php echo('"'.htmlspecialchars ($_POST["fname"]).'"'); ?>
There are many ways:
besides the one nyarathotep mentioned:
echo sprintf('"%s"', $_POST['fname']);
printf('"%s"', $_POST['fname']);
Use this:
echo '"' . $_POST["fname"] . '"';
Or
echo "'" . $_POST["fname"] . "'";
of course if you want to you can replace ' and " with " or ' in the code...