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);
Related
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.
Please take a look at the following situation below.
[reply="292"] Text Here [/reply]
What I am trying to get is the number between the quotations in reply="NUMBERS". I want to extract that to one variable and the text between [reply="NUMBER"] this text here [/reply] to another variable.
So for this example:
[reply="292"] Text Here [/reply]
I want to extract the reply number: 292 and the text between the reply tags: Text here.
I have tried this:
\[reply\=\"]([A-Z]\w)\[\/reply]
But this only works until the reply tag, doesn't work after that. How can I go about doing this?
I left generic (. *), but you can specify a type like decimal (\d+).
php:
$s = '[reply="292"] Text Here [/reply]';
$expr = '/\[reply=\"(.*)\"\](.*)\[\/reply\]/';
if(preg_match($expr,$s,$r)){
var_dump($r);
}
javascript:
s = '[reply="292"] Text Here [/reply]'
s.match(/\[reply=\"(.*)\"\](.*)\[\/reply\]/)
//["[reply="292"] Text Here [/reply]", "292", " Text Here "]
Easy!
\[reply\=\"(\d+)\"](.*?)\[\/reply]
Explanation
\d for digit
+ for 1 or more occurrence of the specified character.
[\w\s] for any character in word and whitespace (\s)
Then apply it to PHP like this:
<?php
$str = "[reply=\"292\"] Text Here [/reply]";
preg_match('/\[reply\=\"(\d+)\"]([\w\s]+)\[\/reply]/', $str, $re);
print_r($re[1]); // printing group 1, the reply number
print_r($re[2]); // printing group 2, the text
?>
Important!!
Just get the group value, not all. You only need some of it anyway.
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>"));
I've got text where some lines are indented with 4 spaces. I've been trying to write a regex which would find every line beginning with 4 spaces and put a <span class="indented"> at the beginning and a </span> at the end. I'm no good at regex yet, though, so it came to nothing. Is there a way to do it?
(I'm working in PHP, in case there's an option easier than regex).
Example:
Text text text
Indented text text text
More text text text
A bit more text text.
to:
Text text text
<span class="indented">Indented text text text</span>
More text text text
A bit more text text.
The following will match lines starting with at least 4 spaces or a tab character:
$str = preg_replace("/^(?: {4,}|\t *)(.*)$/m", "<span class=\"indented\">$1</span>", $str);
I had to do something similar, and one thing I might suggest is changing the goal formatting to be
<span class="tab"></span>Indented text text text
You can then set your css something like .tab {width:4em;} and instead of using preg_replace and regexes, you can do
str_replace($str, " ", "<span class='tab'></span>");
This has the benefit of allowing for 8 spaces to turn into a double width tab easily.
I think this should work:
//get each line as an item in an array
$array_of_lines = explode("\n", $your_string_of_lines);
foreach($array_of_lines as $line) {
// First four characters
$first_four = substr($line, 0, 4);
if($first_four == ' ') {
$line = trim($line);
$line = '<span class="indented">'.$line.'</span>';
}
$output[] = $line;
}
echo implode("\n",$output);
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));