PHP Replace Between Tags - php

I am looking for a quick way to replace text in a string that is between two tags.
The string contains <!-- Model # Start --> <!-- Model # End --> Tags.
I just want to replace what is between the tags, I believe preg_replace() will do this but I am not sure how to make it work.

To use preg_replace, pass in the original string and a regular expression - the matching result will be returned. There is not much more to say about that method as you need to understand regular expressions to use it.
Here is a programatic solution, possibly not the most efficient code, but gives you an indication of what it is doing.
$tagOne = "[";
$tagTwo = "]";
$replacement = "Greg";
$text = "Hello, my name is [NAME]";
$startTagPos = strrpos($text, $tagOne);
$endTagPos = strrpos($text, $tagTwo);
$tagLength = $endTagPos - $startTagPos + 1;
$text = substr_replace($text, $replacement, $startTagPos, $tagLength);
echo $text;
Outputs: Hello, my name is Greg.

$tagOne = "[";
$tagTwo = "]";
$replacement = "Greg";
$text = "Hello, my name is [NAME] endie ho [NAME] \n [NAME]";
$textLength = strlen($text);
for ($i; $i< $textLength; $i++) {
$startTagPos = strrpos($text, $tagOne);
$endTagPos = strrpos($text, $tagTwo);
$tagLength = $endTagPos - $startTagPos + 1;
if ($startTagPos<>0) $text = substr_replace($text, $replacement, $startTagPos, $tagLength);
}
echo $text;
Please note, the if statement that checks if there is a tag pos left, otherwise, for all the textlength - # of tags, your start position of 0, in this case 'H', will get gregs :)
The above outputs
Hello, my name is Greg endie ho Greg Greg

Related

Change the color for first letter in a string php

I just learn PHP by myself so I have many things to ask. I read a sample code about how to change color for the first letter in the string but I don't understand what \1 in a span tag of HTML.I know it is used to change the color to red but why \1 but not another character. Really appreciate your help.
<?php
$text = 'VietJack Team';
$text = preg_replace('/(\b[a-z])/i','<span style="color:red;">\1</span>',$text);
echo $text;
?>
you don't have to use regex, and you shouldn't.
php have a native function called substr_replace, and it gets the job done.
example :
<?php
function str_replace_chr(string $str, int $position, string $format): string
{
$chr = $str[$position - 1];
$p1 = $position - 1;
$p2 = $position - strlen($str);
$replace = sprintf($format,$chr);
return substr_replace($str,$replace,$p1,$p2);
}
echo str_replace_chr('VietJack Team', 1, '<span style="color: red;">%s</span>');
online eval : https://3v4l.org/hvv3p

Remove one extra line break in a text in php

My question is exactly opposite to this one i added last night .
Need to remove the last br tag.
Input:
Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.
Output
Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
My try:
preg_replace("[((?:<br>)+)]","",$posttext)
It removes all breaks.
You can substitute
<br><br>(?!<br)
to <br>
preg_replace('/<br><br>(?!<br)/', "<br>", $posttext);
The lookahead will prevent to match any more <br>
See demo at regex101
Feast your eyes on this hahaha
If Preg replace doesn't work...
// cuts off one <br> as high as whatever $i is at
$string = "Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.";
$i = 10;
while($i > 0)
{
$ii = 1;
$brake = "<br>";
$replace = "";
while($ii < $i)
{
$brake .= "<br>";
$replace .= "<br>";
$ii++;
}
$string = str_replace($brake,$replace,$string);
$i--;
}
echo $string; // Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
PS: If theres no preg replace for this, it is usable albeit very inefficent.

How to break MySQL content(text+html content) into columns?

As a first, I'd like to mention that I understand how everyone feels towards asking questions without providing at least some trial or errors, yet this is purely a need to know knowledge, with that being said, I'll go ahead and ask.
I haven't been able to figure out how to break content let's say 600-1000 text words saved in a MySQL table into 3 sections, so that in between these sections it could include specific java or to make it easier to understand images.
Let's say we retrieve 800 word text from MySQL, the goal would be to do this.
Content
========= Section Break [image] ==========
Content
========= Section Break [image] ==========
I've got this so far, (went ahead and tried to save problems by not including anything), but it is breaking the text in the middle of the sentence, where I'd like to break at least at the last .(dot) on that line.
$text = stripslashes($article_content);
$first = strpos($text," ",strlen($text) / 2);
$col1 = substr($text, 0, $first);
$col2 = substr($text, $first);
echo $col1;
echo "this is the line break";
echo $col2;
and this one it does not print the <br/> ($separator).
$separator = "<br/>";
$text = stripslashes($article_content);
$first = ceil(strlen($text) / 3);
$string = substr($text, 0, $first);
$string = $separator;
$string = substr($text,$first,$first);
$string = $separator;
$string = substr($text,($first*2), $first);
echo $string;
The first I cannot get it to break at the last .(dot) in that line.
It does not print the separator, no idea why either, but the text
prints just fine.
Use a dot before the equalssign after the first instantiation of the variable $string to add to it in your second example.
$separator = "<br/>";
$text = stripslashes($article_content);
$first = ceil(strlen($text) / 3);
$string = substr($text, 0, $first);
$string .= $separator;
$string .= substr($text,$first,$first);
$string .= $separator;
$string .= substr($text,($first*2), $first);
echo $string;

Modify a php limit text function adding some kind of offset to it

Maybe you guys can help:
I have a variable called $bio with bio data.
$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the author of the question";
I search the $bio using a set of functions to search for a certain word, lets say "author" which adds a span class around that word, and I get:
$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the <span class=\"highlight\">author</span> of the question";
I use a function to limit the text to 85 chars:
$bio = limit_text($bio,85);
The problem is when there are more then 80 chars before the word "author" in $bio.
When the limit_text() is applied, I won't see the highlighted word author.
What I need is for the limit_text() function to work as normal, adding all the words that contain the span class highlight at the end.
Something like this:
*"This is the limited text to 85 chars, but there are no words with the span class highlight so I am putting to be continued ... **author**, **author2** (and all the other words that have a span class highlight around them separate by comma "*
Hope you understood what I mean, if not, please comment and I'll try to explain better.
Here is my limit_text() function:
function limit_text($text, $length){ // Limit Text
if(strlen($text) > $length) {
$stringCut = substr($text, 0, $length);
$text = substr($stringCut, 0, strrpos($stringCut, ' '));
}
return $text;
}
UPDATE:
$xturnons = str_replace(",", ", ", $xturnons);
$xbio = str_replace(",", ", ", $xbio);
$xbio = customHighlights($xbio,$toHighlight);
$xturnons = customHighlights($xturnons,$toHighlight);
$xbio = limit_text($xbio,85);
$xturnons = limit_text($xturnons,85);
The customHighlights function which adds the span class highlighted:
function addRegEx($word){ // Highlight Words
return "/" . $word . '[^ ,\,,.,?,\.]*/i';
}
function highlight($word){
return "<span class='highlighted'>".$word[0]."</span>";
}
function customHighlights($searchString,$toHighlight){
$searchFor = array_map('addRegEx',$toHighlight);
$result = preg_replace_callback($searchFor,'highlight',$searchString);
return $result;
}
This change to your limit_text function will take the text, and cut it if it's longer than the given $length. If you pass a $needle to it, it will search for the first occurrence of it, and end your sentence with it.
Also, if the text is cut before it's actual length, it will add $addition to it, while still preserving the limit of $length characters.
I've included a usage and a sample output based on your given below:
<?php
/**
* $text - The text to cut from
* $length - The amount of characters that should be returned
* $needle - If needle is given and found in the text, and it is
* at least $length far from the start of the string - it will end the sentence with it.
* $addition - If the sentence was cut in the middle, will add it to the end of it.
**/
function limit_text($text, $length, $needle="", $addition="...") {
if(strlen($text) > $length) {
$length -= strlen($addition);
$start = 0;
$trimLast = true;
if (!empty($needle)) {
$needleStart = strpos($text, $needle);
if ($needleStart > $length) {
$length -= strlen($needle);
$start = $needleStart + strlen($needle) - $length;
$trimLast = false;
}
}
$stringCut = substr($text, max(0, $start), $length);
if ($start > 0) {
$stringCut = substr($stringCut, strpos($stringCut, ' ')+1);
}
if ($trimLast) {
$lastWhitespace = strrpos($stringCut, ' ');
$stringCut = substr($stringCut, 0, $lastWhitespace);
}
// split into words (so we won't replace words that contain it in the middle)
// and wrap $needle with <span class="highlighted"></span>
if (!empty($needle)) {
$words = explode(" ", $stringCut);
$needles = array_keys($words, $needle);
foreach ($needles as $needleKey) {
$words[$needleKey] = "<span class=\"highlighted\">$needle</span>";
}
$stringCut = implode(" ", $words);
}
$text = $stringCut.$addition;
}
return $text;
}
$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the author of the question";
$text = limit_text($bio, 85, "author");
var_dump($text);
Output:
string (111) "fast cars and boats. I work as a blogger and I'm way cooler then the <span class="highlighted">author</span>..."
First, you need to make sure you don't break words apart by shortening the string. Then you need to append all of the <span class="highlight"> tokens to the end of the shortened string. Here is what I came up with (in about 8 lines!):
function limit_text($text, $length){
if( strlen( $text) < $length) {
return $text;
}
// Truncate the string without breaking words
list( $wrapped) = explode("\n", wordwrap( $text, $length));
// Get the span of text occurring after the wrapped string
$remainder = substr( $text, strlen( $wrapped));
// Add the "to be continued" to $wrapped
$wrapped .= ' to be continued ... ';
// Now, grab all of the <span class="highlight"></span> tags in the $remainder
preg_match_all( '#<span class="highlight">[^<]+</span>#i', $remainder, $matches);
// Add the <span> tags to the end of the string, separated by a comma, if present
$wrapped .= implode( ', ', $matches[0]);
return $wrapped;
}
Now, with your original test:
$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the <span class=\"highlight\">author</span> of the question";
$bio = limit_text( $bio,85);
var_dump( htmlentities( $bio));
This outputs:
string(165) "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way to be continued ... <span class="highlight">author</span>"
Now, another test with multiple <span> tags:
This outputs:
$bio = 'Hello, what about a <span class="highlight">span tag</span> before the limit? Or what if I have many <span class="highlight">span tags</span> <span class="highlight">after</span> <span class="highlight">the</span> limit?';
$bio = limit_text( $bio,85);
var_dump( htmlentities( $bio));
string(308) "Hello, what about a <span class="highlight">span tag</span> before the limit? Or what to be continued ... <span class="highlight">span tags</span>, <span class="highlight">after</span>, <span class="highlight">the</span>"
If you have more test cases, or have a modification to the function above, let me know and I can fix it!
Judging from your requirements, this should do what you want:
function get_highlighted_string($s)
{
return '<span class="highlight">' . htmlspecialchars($s) . '</span>';
}
function limit_text($text, $max_length, array $keywords = array(), $continued = '...')
{
// highlights to put after the cut string
$extra = array();
// highlight keywords
if ($keywords) {
$re = '~\b(' . join('|', array_map('preg_quote', $keywords, array('~'))) . ')\b~i';
// get all matches and capture their positions as well
if (preg_match_all($re, $text, $matches, PREG_OFFSET_CAPTURE)) {
// we reverse the matches by position to make replacement easier
foreach (array_reverse($matches[1]) as $match) {
// $match[0] = match
// $match[1] = start position
$match_len = strlen($match[0]);
if ($match[1] + $match_len <= $max_length) {
// still fits in cut string
$match_replacement = get_highlighted_string($match[0]);
$text = substr_replace($text, $match_replacement, $match[1], $match_len);
// update max length
$max_length = $max_length - $match_len + strlen($match_replacement);
} else {
// will not fit in the cut string, so we place it outside
array_unshift($extra, get_highlighted_string($match[0]));
}
}
}
// use wordwrap and strcspn to cut the string by word boundaries
if (strlen($text) > $max_length) {
$text = substr($text, 0, strcspn(wordwrap($text, $max_length, "\0"), "\0")) . " $continued";
}
}
if ($extra) {
// append what we couldn't fit in the cut string
$text .= ' ' . join(', ', $extra);
}
return $text;
}
Example:
echo limit_text("Hello, I like fast cars and boats. I work as a blogger I'm way cooler then the author of the question", 85, array('author', 'question'));
Hello, I like fast cars and boats. I work as a blogger I'm way cooler then the <span class="highlight">author</span> ... <span class="highlight">question</span>
In the example, the cut-off is exactly at author so that highlight comes before the ... while the question keywords gets put behind.
Another example:
echo limit_text("Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the author of the question", 85, array('author', 'question'));
Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way ... <span class="highlight">author</span>, <span class="highlight">question</span>
Both keywords are beyond the 85 character marker, so they are appended at the back, comma separated.
Let me know if this works for you :)

Replace text ignoring HTML tags

I have a simple text with HTML tags, for example:
Once <u>the</u> activity reaches the resumed state, you can freely add and remove fragments to the activity. Thus, <i>only</i> while the activity is in the resumed state can the <b>lifecycle</b> of a <hr/> fragment change independently.
I need to replace some parts of this text ignoring its html tags when I do this replace, for example this string - Thus, <i>only</i> while I need to replace with my string Hello, <i>its only</i> while . Text and strings to be replaced are dynamically. I need your help with my preg_replace pattern
$text = '<b>Some html</b> tags with <u>and</u> there are a lot of tags <i>in</i> this text';
$arrayKeys= array('Some html' => 'My html', 'and there' => 'is there', 'in this text' => 'in this code');
foreach ($arrayKeys as $key => $value)
$text = preg_replace('...$key...', '...$value...', $text);
echo $text; // output should be: <b>My html</b> tags with <u>is</u> there are a lot of tags <i>in</i> this code';
Please help me to find solution. Thank you
Basically we're going to build dynamic arrays of matches and patterns off of plain text using Regex. This code only matches what was originally asked for, but you should be able to get an idea of how to edit the code from the way I've spelled it all out. We're catching either an open or a close tag and white space as a passthru variable and replacing the text around it. This is setup based on two and three word combinations.
<?php
$text = '<b>Some html</b> tags with <u>and</u> there are a lot of tags <i>in</i> this text';
$arrayKeys= array(
'Some html' => 'My html',
'and there' => 'is there',
'in this text' =>'in this code');
function make_pattern($string){
$patterns = array(
'!(\w+)!i',
'#^#',
'! !',
'#$#');
$replacements = array(
"($1)",
'!',
//This next line is where we capture the possible tag or
//whitespace so we can ignore it and pass it through.
'(\s?<?/?[^>]*>?\s?)',
'!i');
$new_string = preg_replace($patterns,$replacements,$string);
return $new_string;
}
function make_replacement($replacement){
$patterns = array(
'!^(\w+)(\s+)(\w+)(\s+)(\w+)$!',
'!^(\w+)(\s+)(\w+)$!');
$replacements = array(
'$1\$2$3\$4$5',
'$1\$2$3');
$new_replacement = preg_replace($patterns,$replacements,$replacement);
return $new_replacement;
}
foreach ($arrayKeys as $key => $value){
$new_Patterns[] = make_pattern($key);
$new_Replacements[] = make_replacement($value);
}
//For debugging
//print_r($new_Patterns);
//print_r($new_Replacements);
$new_text = preg_replace($new_Patterns,$new_Replacements,$text);
echo $new_text."\n";
echo $text;
?>
Output
<b>My html</b> tags with <u>is</u> there are a lot of tags <i>in</i> this code
<b>Some html</b> tags with <u>and</u> there are a lot of tags <i>in</i> this text
Here we go. this piece of code should work, assuming you're respecting only twp constraints :
Pattern and replacement must have the same number of words. (Logical, since you want to keep position)
You must not split a word around a tag. (<b>Hel</b>lo World won't work.)
But if these are respected, this should work just fine !
<?php
// Splits a string in parts delimited with the sequence.
// '<b>Hey</b> you' becomes '~-=<b>~-=Hey~-=</b>~-= you' that make us get
// array ("<b>", "Hey" " you")
function getTextArray ($text, $special) {
$text = preg_replace ('#(<.*>)#isU', $special . '$1' . $special, $text); // Adding spaces to make explode work fine.
return preg_split ('#' . $special . '#', $text, -1, PREG_SPLIT_NO_EMPTY);
}
$text = "
<html>
<div>
<p>
<b>Hey</b> you ! No, you don't have <em>to</em> go!
</p>
</div>
</html>";
$replacement = array (
"Hey you" => "Bye me",
"have to" => "need to",
"to go" => "to run");
// This is a special sequence that you must be sure to find nowhere in your code. It is used to split sequences, and will disappear.
$special = '~-=';
$text_array = getTextArray ($text, $special);
// $restore is the array that will finally contain the result.
// Now we're only storing the tags.
// We'll be story the text later.
//
// $clean_text is the text without the tags, but with the special sequence instead.
$restore = array ();
for ($i = 0; $i < sizeof ($text_array); $i++) {
$str = $text_array[$i];
if (preg_match('#<.+>#', $str)) {
$restore[$i] = $str;
$clean_text .= $special;
}
else {
$clean_text .= $str;
}
}
// Here comes the tricky part.
// We wanna keep the position of each part of the text so the tags don't
// move after.
// So we're making the regex look like (~-=)*Hey(~-=)* you(~-=)*
// And the replacement look like $1Bye$2 me $3.
// So that we keep the separators at the right place.
foreach ($replacement as $regex => $newstr) {
$regex_array = explode (' ', $regex);
$regex = '(' . $special . '*)' . implode ('(' . $special . '*) ', $regex_array) . '(' . $special . '*)';
$newstr_array = explode (' ', $newstr);
$newstr = "$1";
for ($i = 0; $i < count ($regex_array) - 1; $i++) {
$newstr .= $newstr_array[$i] . '$' . ($i + 2) . ' ';
}
$newstr .= $newstr_array[count($regex_array) - 1] . '$' . (count ($regex_array) + 1);
$clean_text = preg_replace ('#' . $regex . '#isU', $newstr, $clean_text);
}
// Here we re-split one last time.
$clean_text_array = preg_split ('#' . $special . '#', $clean_text, -1, PREG_SPLIT_NO_EMPTY);
// And we merge with $restore.
for ($i = 0, $j = 0; $i < count ($text_array); $i++) {
if (!isset($restore[$i])) {
$restore[$i] = $clean_text_array[$j];
$j++;
}
}
// Now we reorder everything, and make it go back to a string.
ksort ($restore);
$result = implode ($restore);
echo $result;
?>
Will output Bye me ! No, you don't need to run!
[EDIT] Now supporting a custom pattern, which allows to avoid adding useless spaces.

Categories