Hi I have the following situation where I have tags wrapped around my contents and the following input
[hello]
Text here
[/hello]
[hello]
Text here 2
[/hello]
The output is
[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]
The desired output should be
[hello]\nText here\n[/hello][hello]\nText here 2\n[/hello]
PS: THanks for the answer, but since its user input, there's a chance of spacing [hello] \n
Is there a way using php, to trim the first \n around the opening tag ([hello]) and closing tag([/hello])? Thanks
it's simple, in the other words you want to replace \n\n to \n
$str = "[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]";
echo str_replace("\n\n", "\n", $str);
it will output
[hello]
Text here
[/hello]
[hello]
Text here 2
[/hello]
Its simple, Try This:
$str = "[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]";
echo str_replace("[hello]\n","[hello]", str_replace("\n[/hello]","[/hello]",$str));
Related
I want to change Text1 to Text2.
Text1
Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
Text2
Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.
i.e; add extra 'breakline' tag to the existing one in a string.
I tried it with preg_replace but can't figure it the way I wanted.
My Try -
preg_replace('/(?:(?:<br>)\s*)/s', "<br><br>", $posttext)
This should do it:
$text = preg_replace('/((<br>(\s+)?)+)/', '$1<br>', $text);
If you don't want to allow for newlines and spaces try: /((<br>)+)/
Try this:
preg_replace('/((?:<br>)+)\s*/s', "$1<br>", $posttext);
This captures a sequence of <br> tags, optionally followed by whitespace, and then adds one more after them.
DEMO
try this.
$text1 = "Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.";
$text2 = substr($text1,0,strripos($text1,"<br>")) ."<br>" . substr($text1,strripos($text1,"<br>"));
How can I find and replace the same characters in a string with two different characters? I.E. The first occurrence with one character, and the second one with another character, for the entire string in one go?
This is what I'm trying to do (so users need not type html in the body): I've used preg_replace here, but I'll willing to use anything else.
$str = $str = '>>Hello, this is code>> Here is some text >>This is more code>>';
$str = preg_replace('#[>>]+#','[code]',$str);
echo $str;
//output from the above
//[code]Hello, this is code[code] Here is some text [code]This is more code[code]
//expected output
//[code]Hello, this is code[/code] Here is some text [code]This is more code[/code]
But problem here is, both >> get replaced with [code]. Is it possible to somehow replace the first >> with [code] and the second >> with a [/code] for the entire output?
Does php have something to do this in one go? How can this be done?
$str = '>>Hello, this is code>> Here is some text >>This is more code>>';
echo preg_replace( "#>>([^>]+)>>#", "[code]$1[/code]", $str );
The above will fail if something like the following is your input:
>>Here is code >to break >stuff>>
To deal with this, use negative lookahead:
#>>((?!>[^>]).+?)>>#
will be your pattern.
echo preg_replace( "#>>((?!>[^>]).+?)>>#", "[code]$1[/code]", $str );
I'm programming a wiki with BBCode-like editing syntax.
I want the user to be allowed to enter line breaks that resolve to <br> tags.
Until here there's no problem occuring.
Now i also have the following lines, that should convert into a table:
[table]
[row]
[col]Column1[/col]
[col]Column2[/col]
[col]Column3[/col]
[/row]
[/table]
All those line breaks, that were entered when formatting the editable BBCode above are creating <br> tags that are forced to be rendered in front of the html-table.
My goal is to remove all line breaks between [table] and [/table] in my parser function using php's preg_replace without breaking the possibility to enter normal text using newlines.
This is my parsing function so far:
function richtext($text)
{
$text = htmlspecialchars($text);
$expressions = array(
# Poor attempts
'/\[table\](\r\n*)|(\r*)|(\n*)\[\/table\]/' => '',
'/\[table\]([^\n]*?\n+?)+?\[\/table\]/' => '',
'/\[table\].*?(\r+).*?\[\/table\]/' => '',
# Line breaks
'/\r\n|\r|\n/' => '<br>'
);
foreach ($expressions as $pattern => $replacement)
{
$text = preg_replace($pattern, $replacement, $text);
}
return $text;
}
It would be great if you could also explain a bit what the regex is doing.
Style
First of all, you don't need the foreach loop, preg_replace accepts mixed variables, e.g. arrays, see Example #2: http://www.php.net/manual/en/function.preg-replace.php
Answer
Use this regex to remove all line breaks between two tags (here table and row):
(\[table\]([^\r\n]*))(\r\n)*([^\r\n]*\[row\])
The tricky part is to replace it (See also this: preg_replace() Only Specific Part Of String):
$result = preg_replace('/(\[table\][^\r\n]*)(\r\n)*([^\r\n]*\[row\])/', '$1$4', $subject);
Instead of replacing with '', you replace it only the second group ((\r\n)*) with '$1$4'.
Example
[table] // This will also work with multiple line breaks
[row]
[col]Column1[/col]
[col]Column2[/col]
[col]Column3[/col]
[/row]
[/table]
With the regex, this will output:
[table] [row]
[col]Column1[/col]
[col]Column2[/col]
[col]Column3[/col]
[/row]
[/table]
I've been producing a letter compilation system (to save people time after a questionaire has been filled in) and near the end of the project we've found a bug. Long story short it would take many hours to fix without this regular expression - which is why I'm asking for your fine help!
We have some text that contains the following...
"<k>This is the start of the paragraph
This is some more of the paragraph.
And some more";
I basically need a regular expression that can search for the opening tag, "<k>", and also the first new line it comes across "\r\n"? and then insert the contents into a variable I can then use (with the <k> removed but the new line codes, "\r\n", left in place).
I'm using PHP and the text (like the example above) is stored in MySQL.
Please help!
I promise I'll learn these properly after I've fixed this bug! :)
If you are using 5.3 you can make some use of closures like this:
$text = "<k>This is the start of the paragraph
This is some more of the paragraph.
And some more";
$matches = array();
$text = preg_replace_callback('/<k>(.*)$/m', function($match) use (&$matches){
$matches[] = $match[1];
}, $text);
var_dump($text,$matches);
Output is:
string '
This is some more of the paragraph.
And some more' (length=52)
array
0 => string 'This is the start of the paragraph' (length=34)
I'm assuming there could be multiple <k> tags in the text, and therefore, I put all the text that follows the tag into an array called matches.
As a further example... with the following input:
$text = "<k>This is the start of the paragraph
Line 2 doesn't have a tag...
This is some more <k>of the paragraph.
Line 4 doesn't have a tag...
<k>And some more";
The output is:
string '
Line 2 doesn't have a tag...
This is some more
Line 4 doesn't have a tag...
' (length=78)
array
0 => string 'This is the start of the paragraph' (length=34)
1 => string 'of the paragraph.' (length=17)
2 => string 'And some more' (length=13)
/^<k>(\w*\s+)$/
would probably work.
I have a string thats separated by a space. I want to show every part of the string on new line that is separated by space. how can I do that.
base1|123|wen dsj|test base2|sa|7243|sdg custom3|dskkjds|823|kd
if there is no more | after an initial pipe then the space should break the line and it should look like this
base1|123|wen dsj|test
base2|sa|7243|sdg
custom3|dskkjds|823|kd
echo str_replace(' ',"\n",$string);
or
echo str_replace(' ',PHP_EOL,$string);
This is pretty messy, yet to clean up the last empty result:
$string = 'base1|123|wen dsj|test base2|sa|7243|sdg custom3|dskkjds|823|kd';
preg_match_all('/(?P<line>(?:[^\\| ]*\\|{0,1})*(?: [^\\| ]*\\|[^\\| ]*(?: |\\z){0,1})*)(?: |\\z)/',$string,$matches,PREG_SET_ORDER);
print_r($matches);
Edit: Actually this is pretty horrible