PHP add text before and after an arrays elements - php

How can I wrap <i> </i> tags around This is line one!, This is line two!, This is line three! in $thisStr below, so the result is:
`<i>This is line one!</i>`
`<i>This is line two!</i>`
`<i>This is line three!</i>`
What I did only puts tags around the :: parts. I cant get it to go back and forward and wrap tags.
$thisStr = 'This is line one! :: This is line two! :: This is line three!';
echo preg_replace("/(::)/i", "<i>$1</i>", $thisStr);

This works great, but is not preg_replace:
$thisStr = 'This is line one! :: This is line two! :: This is line three!';
$result= "<i>".str_replace("::","</i><i>",$thisStr)."</i>";
echo $result;
Or, to match your exact example:
$thisStr = 'This is line one! :: This is line two! :: This is line three!';
$result= "'<i>".str_replace(" :: ","</i>'<br/>'<i>",$thisStr)."</i>'";
echo $result;
/*
'This is line one!'
'This is line two!'
'This is line three!'
*/

If you really really really need to use preg_replace this will work
$input_lines = "This is line one!
This is line two!
This is line three!";
$New_string = preg_replace("/This is line .*/", "<i>$0</i>", $input_lines);
The pattern looks for the string "this is line" and the .* means anything in any lenght.
Then the replace pattern is quite easy to understand I guess.
But i recommend the answer Verjas wrote.
There is no need for the extra complexty the preg_replace adds.

Try this:
$thisStr = 'This is line one! :: This is line two! :: This is line three!';
$lines = explode('::', $thisStr);
$result = '';
foreach($lines as $line) {
$result .= '<i>' . $line .'</i>';
}
echo htmlentities($result);
Hope this helps.

Related

PHP 8.1 preg_replace only outside of two UBB tags - regex probably wrong

I need to replace the line break \n or \r with the HTML tag <br>, but only outside of [code] UBB tags.
Example string, how it is stored in the DB:
$string = "Test 123
Line break
[code]5: Test
10: With line
15: breaks[/code]
Further writing
Another new line";
Using this code:
$bbextended = array(
"/\[code\](.*?)\[\/code\](*SKIP)(*FAIL)|\\r?\\n/i" => "<br>",
"/\[code\](.*?)\[\/code\]/i" => "<textarea>$1</textarea>",
);
foreach($bbextended as $match=>$replacement){
$string = preg_replace($match, $replacement, $string);
}
echo $string;
I end up with this HTML output:
Line break<br>Test123<br>[code]5: Test<br>10: With line<br>15: breaks[/code]<br>Further writing<br>Another new line
Which means that line breaks within the [code][/code] UBB tags were replaced with <br> (wrong, should not) and the code elements were not replaced by <textarea></textarea> (also wrong).
The expected HTML output is:
Line break<br>Test123<br><textarea>5: Test
10: With line
15: breaks
</textarea><br>Further writing<br>Another new line
I tried to solve it on my own with regex101, but it highlights stuff that shouldnt be highlighted..
I assume there is an issue caused by the actual line breaks and how regex searches.
"/[code[\w\W]*?code](*SKIP)(*FAIL)|\\r?\\n/i" => "<br>",
this seems to work - to detect start and end of my code tags across multiple lines and skip them from formatting with

PHP Remove Unused Line Breaks Using nl2br

How to remove unused line breaks using nl2br function for this example:
Hello
Nice
Expect output display:
Hello
Nice
and another example:
remove this unused line
remove this unused line
remove this unused line
Hello
remove this unused line
remove this unused line
remove this unused line
remove this unused line
Expect output display:
Hello
So means if the line break more than 3 line, so only set 1 line breaks.
Here is my PHP code:
nl2br($string);
Old schooled but hope this works
$str = explode("\n", $str);
foreach($str as $val){
if(!empty($val)){
$result[] = $val;
}
}
$final = implode("\n", $result); //if you want line break use "<br>"
echo $final;

How to add symbol in each end of the line in php?

Example
$string = "Test Line 1
Test Line 2
Test Line 3";
echo str_replace(PHP_EOL,"/".PHP_EOL, $string );
Actually I am expecting output is
Test Line 1 \
Test Line 2 \
Test Line 3 \
Please help me to solve this problem
Try this,
Edited:
<?php
$string = "Test Line 1
Test Line 2
Test Line 3";
$str = str_replace(PHP_EOL,"\<br />", $string) . " \\";
echo $str;
?>
Use \\ instead of "/"
<?php
$string = "Test Line 1
Test Line 2
Test Line 3";
$var=str_replace(PHP_EOL,"\\"."</br>", $string );
echo $var." \\"; // this append \ in last string
?>
If this is just so that you can put the php value into a JS variable, you can just use json_encode() which will change the new lines to \n for you
$string = "Test Line 1
Test Line 2
Test Line 3";
echo json_encode($string);
// outputs(including quotes) "Test Line 1 \nTest Line 2 \nTest Line 3"

Line breaks from textarea and mysql dont appear in html

How can I make text appear on diff lines in html?! They appear on diff lines in my text area when output from mysql, and also appear on diff lines inside mysql. But in the web page its all on one line. How can this be solved?
Use:
string nl2br ( string $string [, bool $is_xhtml = true ] )
It can handle \n, \r, \r\n or \n\r linebreaks and replaces them with a <br />
Example:
$yourText = "This is line one.\nThis is line two.";
$yourText = nl2br($yourText);
echo $yourText;
This will result in:
This is line one.<br />This is line two.
Link to the manual for more information.
Use the PHP nl2br function:
$string = "hello \n world";
$string = nl2br($string);
It's quite self-explanitory: \n gets replaced with <br />
Replace Linebreaks with <br>
str_replace(.N, '<br>', [element]);
You can use <pre>..Your text..</pre> tag for that.

Capture new line in a string

I know for sure that this was already been asked before but I just googled around and couldn't find anything (maybe wrong word choice?).
Just don't be too mad at me, I'm getting mad too...
I'd like
$string = '
This is a line
This is another line
';
to be shown to the HTML page as
This is a line
This is another line
when I do echo $string;.
How can I capture the return key or the new line and replace it with <br>?
Try the nl2br() function:
echo nl2br($string);
Would return:
<br>
This is a line<br>
This is another line<br>
To trim off the leading and trailing new lines, use trim():
echo nl2br(trim($string));
Would return:
This is a line<br>
This is another line
You can use the PHP function nl2br. It doesn't replace the newlines but, rather, inserts <br /> next to them (which is perfectly fine for your purposes).
Using your example:
$string = '
This is a line
This is another line
';
echo nl2br($string);
/* output
<br />
This is a line<br />
This is another line<br />
*/
Use nl2br function like this:
echo nl2br($string);
If you're not getting it with nl2br, your new line character must not be \n.
print nl2br( str_replace( array( "\r\n", "\r" ), "\n", $string);
Why don't you use:
$string = "
This is a line\n
This is another line
";
?
or use
$string = <<<EOF
This is a line
This is another line
EOF;

Categories