Add one extra line break in a text in php - php

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>"));

Related

PHPWord/TemplateProcessor How to make paragraphs

I have text in a with paragraphs, spaces and justied text like this coming from the database.
Hello.
My name is John.
Thank you
But when I use PHP Word with TemplateProcessor to move to a Word document it generates everything without paragraphs.
One solution I found for this was to do this:
$text=preg_replace('/\v+|\\\r\\\n/','<w:p/>',$TextWhitoutParagraphs);
He actually writes with paragraphs but only the first paragraph is justified.
How do I do this correctly with paragraphs and all text justified?
You can use cloneBlock. I used it with justified text and it worked perfectly. In your template use:
${paragraph}
${text}
${/paragraph}
And then explode your string by"\n":
$textData = explode("\n", $text);
$replacements = [];
foreach($textData as $text) {
$replacements[] = ['text' => $text];
}
$templateProcessor->cloneBlock('paragraph', count($replacements), true, false, $replacements);
The TemplateProcessor can only be used with single line strings (see docs: "Only single-line values can be replaced." http://phpword.readthedocs.io/en/latest/templates-processing.html)
What you could try is replacing your new lines with '' (closing the opened paragraph and starting a new one), but that would just be my guess right now. It always helps to check the resulting Word-XML for syntax-errors.

PHP replace : find and replace the same characters with different text

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 );

how to remove ♥ (or ♥) from a string in php?

this text block represents the original external source that is a variable:
// the following characters are not-consistent
text here
// the following characters are consistent
♥ text1
♥ text2
♥ text3
// the following characters are consistent
some more text here
i want to remove everything except for "text here" and i have to do this in 'parts' as other text may be present (ie i can't just remove everything after "text here").
i can remove "some more text here" but am having trouble trying to target and remove the lines beginning with ♥ (or ♥ as they are currently being stored in the database).
so these are my variables
// this is the original external source
$part_one = $external_source;
// this is the variable that is not working yet
$part_one_a = ;
// this replaces "some more text here" with nothing
$part_one_b = str_replace("some more text here", " ", $part_one);
// this concatenates the variables $part_one_a and $part_one_b
$final = $part_one_a . $part_one_b;
thank you.
update
to be more specific i need a way to remove text such as this:
♥ link_one: http://link_one_here.com/
♥ link_two: http://link_two_here.com/
♥ link_three: http://link_three_here.com/
update two
i've managed to remove all unwanted items with str_replace on an array.
the only thing i cannot target and therefore remove is the ♥.
i have tried targeting it with:
♥ (this is the value that is currently being stored in the
database)
♥ (after i applied a htmlentities to the string)
and no luck - the ♥ is still being displayed. if anyone can tell me how i can target ♥ it would be much appreciated and this question would be resolved.
thank you.
If i understand you correctly you need the all text here which is before ♥ you can use strpos
$stopper = '♥';
$string = 'text here
♥ text1
♥ text2
♥ text3
some more text here' ;
$final = trim(substr($string,0,strpos($string, $stopper)));
var_dump($final);
Output
string 'text here' (length=9)
Please note that your $stopper can also be \n new line
the solution was:
Do a str_replace on an array to target and remove specific portions of text.
Create a regex and define acceptable values.
Do a preg_replace on non-acceptable values.
this resulted in all the desired values being shown without instances of non-acceptable characters.
the code for the last two steps is below:
// this is the original external source
$part_one = $external_source;
// create a regex
$regex
= '~'
. '['
. '^'
. 'A-Z'
. '0-9'
. ' '
. '!_:/,.#$-'
. ']'
. '~'
. 'i'
;
// clean the string
$part_one_cleaned = preg_replace($regex, NULL, $part_one);

Remove the first instance of '\n' around a string

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));

replace space with new line

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

Categories