php: replacing double <br /> with </p><p> - php

i use nicEdit to write RTF data in my CMS. The problem is that it generates strings like this:
hello first line<br><br />this is a second line<br />this is a 3rd line
since this is for a news site, i much prefer the final html to be like this:
<p>hello first line</p><p>this is a second line<br />this is a 3rd line</p>
so my current solution is this:
i need to trim the $data for <br /> at the start/end of the string
replace all strings that have 2 <br/> or more with </p><p> (one single <br /> is allowed).
finally, add <p> at the start and </p> at the end
i only have steps 1 and 3 so far. can someone give me a hand with step 2?
function replace_br($data) {
# step 1
$data = trim($data,'<p>');
$data = trim($data,'</p>');
$data = trim($data,'<br />');
# step 2 ???
// preg_replace() ?
# step 3
$data = '<p>'.$data.'</p>';
return $data;
}
thanks!
ps: it would be even better to avoid specific situations. example: "hello<br /><br /><br /><br /><br />too much space" -- those 5 breaklines should also be converted to just one "</p><p>"
final solution (special thanks to kemp!)
function sanitize_content($data) {
$data = strip_tags($data,'<p>,<br>,<img>,<a>,<strong>,<u>,<em>,<blockquote>,<ol>,<ul>,<li>,<span>');
$data = trim($data,'<p>');
$data = trim($data,'</p>');
$data = trim($data,'<br />');
$data = preg_replace('#(?:<br\s*/?>\s*?){2,}#','</p><p>',$data);
$data = '<p>'.$data.'</p>';
return $data;
}

This will work even if the two <br>s are on different lines (i.e. there is a newline or any whitespace between them):
function replace_br($data) {
$data = preg_replace('#(?:<br\s*/?>\s*?){2,}#', '</p><p>', $data);
return "<p>$data</p>";
}

This approach will solve your problem:
Split the string on <br> or <br />: you'll get an array of strings.
Create a new string <p>.
Loop on the array of 1, from the beginning to the end and remove all entries that are empty, until an entry that is not empty (break).
Same as 3, but from the end to the beginning of the array.
Loop on the array of 1, have an integer value A (default 0), which states that there is a single or double break.
If the string is empty, increase the value of A and continue the loop.
If the string is not empty:
If the value of A is 1 or below, append a <br>.
If the value of A is 2 or above, append a </p><p>.
Append the content of the current entry (which is not empty).
Set the value of A to 0.
Append </p>
A different approach: using Regular Expressions
(<br ?/?>){2,}
Will match 2 or more <br>. (See php.net on preg_split on how to do this.)
Now, the same approach on step 2 and 3: loop on the array twice, once from the beginning up (0..length) and once from the end down (length-1..0). If the entry is empty, remove it from the array. If the entry is not empty, quit the loop.
To do this:
$array = preg_split('/(<br ?/?>\s*){2,}/i', $string);
foreach($i = 0; $i < count($array); $i++) {
if($value == "") {
unset($array[$i]);
}else{
break;
}
}
foreach($i = count($array) - 1; $i >= 0; $i--) {
if($value == "") {
unset($array[$i]);
}else{
break;
}
}
$newString = '<p>' . implode($array, '</p><p>') . '</p>';

I think this should work for step #2 unless I am not understanding your scenario completely:
$string = str_replace( '<br><br>', '</p><p>', $string );
$string = str_replace( '<br /><br />', '</p><p>', $string );
$string = str_replace( '<br><br />', '</p><p>', $string );
$string = str_replace( '<br /><br>', '</p><p>', $string );

Related

php string manipulation issues

I have the following string...
$string = "True is True (5-7 years)";
what I want is to get - TiT(5-7 years)
I have tried the following code but no luck...
$string = "True is True (5-7 years)";
$explodedString = explode(" ",$string);
for($i = 0; $i < 4; $i++){
$tempString = substr($explodedString[$i], 0, 1);
$finalString .= $tempString;
}
In short, I need the first three words of its initials and the remaining in bracket is as it is like this.... TiT(5-7 years). how?
This a good case for using regular expressions:
$str = 'True is True (5-7 years)';
preg_match_all('~\([^()]*\)|\b\w~', $str, $matches);
echo implode("", $matches[0]); // TiT(5-7 years)
Regex breakdown:
\([^()]*\) Match anything inside parentheses including themselves
| Or
\b\w Match first word character from a word
Your loop is going one element too far. If you want the first letter of the first 3 words, it should be $i < 3.
Then you should use array_slice() and implode() to concatenate the rest of the array.
for ($i = 0; $i < 3; $i++) {
$finalString .= $explodedString[$i][0];
}
$finalString .= implode(' ', array_slice($explodedString, 3));
DEMO
$string = "True is True (5-7 years)";
$new_string = preg_replace('/^([a-z])[a-z]+ ([a-z])[a-z]+ ([a-z])[a-z]+ (\(.+\))$/i', '$1$2$3$4', $string);
First of all.
Create an empty variable. That will be your final result
$result="";
Then youse foreach to loop your explode string.
At every part chech the first character.
If it's not ( add the first char onto the result variable.
else add the whole array element onto the result variable
foreach(explodedString as $t){
If($t[0] !="("){$result.=$t[0];} else{$result.=$t;}
}
At the end of the loop you will get what you wanted
echo $result;

PHP Find two consecutive numeric entries in array

My problem is it just replicates the number twice, though it does add the break when there is a number, but I'm trying to check if there is a number after the number, so it would say line 12 is.....
Thanks for any help
<?PHP
$lines = file_get_contents('http://www.webstitcher.com/test.txt');
$tag = str_split($lines); // puts all lines into a array
foreach ($tag as $num => $letta){
if (is_numeric($letta) == TRUE){
$num2 = $num++;
if (is_numeric($tag[$num2])){ // checks if next line is going to be another digit
$letta .= $tag[$num2];
unset($tag[$num2]); // removes line if it had another digit and adds to ouput
}
echo '<br />' . $letta;
}
else {
echo $letta;
}
}
?>
Try exploding the string using ' ' as the delimiter. This will allow you to keep the numbers whole and will ultimately help cut out a lot of the complexity.
$lines = file_get_contents('http://www.webstitcher.com/test.txt');
$tag = explode(' ', $lines); // puts all words into a array
foreach ($tag as $word){
if (is_numeric($word)) {
// if the word is numeric, simply skip to next line
// if you need to keep the number, add $word to the echo statement
echo '<br />';
}
else {
echo ' '.$word;
}
}
This way you don't have to keep track of the previous element in the array or check for the next element.
Alternatively, you could also use preg_replace which would remove the need for the loop entirely.
$lines = preg_replace('/[0-9]+/', '<br>', $words);

Explode php string on X occurrence of a specific word

How do you select the content of a string based on a changing count?Each time the loop is run the count increments by 1 and the next portion of the string is required.
$mystring = 'This is my string. This string is a sample. This is a problem';
So if $i==1 then I want
echo $newstring // This is my string.
At $i==2 I want
echo $newstring // This string is a sample.
At $i==3 I want
echo $newstring // This is a problem.
I have looked at lots of reference pages on explode, substr, array_pop etc but I haven't seen a method that allows for the position of the trigger word to change based on an incrementing counter.
This could be answered with Explode a paragraph into sentences in PHP
foreach (preg_split('/[.?!]/',$mystring) as $sentence) {
echo $sentence;
}
Also you can access each element:
$matches = preg_split('/[.?!]/',$mystring);
echo $matches[0]; // This is my string
echo $matches[1]; // This string is a sample
echo $matches[2]; // This is a problem
If . is the part where you want to explode the string then you can use regular expression.
$line = 'This is my string. This string is a sample. This is a problem.';
preg_match("/([[:alpha:]|\s]+\.)/i", $line, $match);
echo $match[1];
Example
https://regex101.com/r/4SHAJj/1
I found a solution to this, and although it may not be the cleanest or best way, it does work.
$shippingData contains
Shipping (<div class="AdvancedShipperShippingMethodCombination"><p class="AdvancedShipperShippingMethod">Free Shipping <br />1 x IARP IA313200 Door Gasket</p> <p class="AdvancedShipperShippingMethod">Economy Delivery (1Kg) <br />1 x WIP69457. Whirlpool Part Number 481946669457</p></div>)';
Code used:
$shippingData = $order_result->fields['shipping_method'];
$matches = preg_split("/(AdvancedShipperShippingMethod\">)/", $shippingData);
$method = $matches[$n]; //$n is a count that increments with while/next loop
$method = substr($method, 0, strpos($method, "<br />"));
$method = "Shipping (".$method.")";
}

adding link values around each string

I have a php value coming back from my database as a string, like
"this, that, another, another"
And I'm trying to wrap a separate link around each of those strings, but I can't seem to get it to work. I've tried a for loop, but since it's just a string of information and not an array of information that doesn't really work. Is there a way to wrap a unique link around each value in my string?
The easiest way that I see to do this would be using PHP's explode() function. You'll find that it will become very useful as you start to use PHP more and more, so do check out its documentation page. It allows you to split a string up into an array given a certain separator. In your case, this would be ,. So to split the string:
$string = 'this, that, another, another 2';
$parts = explode(', ', $string);
Then use a foreach (again, check the documentation) to iterate through each of the parts and make them into a link:
foreach($parts as $part) {
echo '' . $part . "\n";
}
However, you can do this with a for loop. Strings can be accessed like arrays, so you can implement a parser pattern to parse the string, extract the parts, and create the links.
// Initialize some vars that we'll need
$str = "this, that, another, another";
$output = ""; // final output
$buffer = ""; // buffer to hold current part
// Iterate over each character
for($i = 0; $i < strlen($str); $i++) {
// If the character is our separator
if($str[$i] === ',') {
// We've reached the end of this part, so add it to our output
$output .= '' . trim($buffer) . "\n";
// clear it so we can start storing the next part
$buffer = "";
// and skip to the next character
continue;
}
// Otherwise, add the character to the buffer for the current part
$buffer .= $str[$i];
}
echo $output;
(Codepad Demo)
A better way is to do it like this
$string = "this, that, another, another";
$ex_string = explode(",",$string);
foreach($ex_string AS $item)
{
echo "<a href='#'>".$item."</a><br />";
}
First explode the string to get the individual words in an array. Then add the hyperlinks to the words and finally implode them.
$string = "this, that, another, another";
$words = explode(",", $string);
$words[0] = $words[0]
$words[1] = $words[1]
..
$string = implode(",", $words);
You can also use the for loop to assign hyperlinks that follow a pattern like this:
for ($i=0; $i<count($words); $i++) {
//assign URL for each word as its name or index
}

echo partial text

I want to display just two lines of the paragraph.
How do I do this ?
<p><?php if($display){ echo $crow->content;} ?></p>
Depending on the textual content you are referring to, you might be able to get away with this :
// `nl2br` is a function that converts new lines into the '<br/>' element.
$newContent = nl2br($crow->content);
// `explode` will then split the content at each appearance of '<br/>'.
$splitContent = explode("<br/>",$newContent);
// Here we simply extract the first and second items in our array.
$firstLine = $splitContent[0];
$secondLine = $splitContent[1];
NOTE - This will destroy all the line breaks you have in your text! You'll have to insert them again if you still want to preserve the text in its original formatting.
If you mean sentences you are able to do this by exploding the paragraph and selecting the first two parts of the array:
$array = explode('.', $paragraph);
$2lines = $array[0].$array[1];
Otherwise you will have to count the number of characters across two lines and use a substr() function. For example if the length of two lines is 100 characters you would do:
$2lines = substr($paragraph, 0, 200);
However due to the fact that not all font characters are the same width it may be difficult to do this accurately. I would suggest taking the widest character, such as a 'W' and echo as many of these in one line. Then count the maximum number of the largest character that can be displayed across two lines. From this you will have the optimum number. Although this will not give you a compact two lines, it will ensure that it can not go over two lines.
This is could, however, cause a word to be cut in two. To solve this we are able to use the explode function to find the last word in the extracted characters.
$array = explode(' ', $2lines);
We can then find the last word and remove the correct number of characters from the final output.
$numwords = count($array);
$lastword = $array[$numwords];
$numchars = strlen($lastword);
$2lines = substr($2lines, 0, (0-$numchars));
function getLines($text, $lines)
{
$text = explode("\n", $text, $lines + 1); //The last entrie will be all lines you dont want.
array_pop($text); //Remove the lines you didn't want.
return implode("<br>", $text); //Implode with "<br>" to a string. (This is for a HTML page, right?)
}
echo getLines($crow->content, 2); //The first two lines of $crow->content
Try this:
$lines = preg_split("/[\r\n]+/", $crow->content, 3);
echo $lines[0] . '<br />' . $lines[1];
and for variable number of lines, use:
$num_of_lines = 2;
$lines = preg_split("/[\r\n]+/", $crow->content, $num_of_lines+1);
array_pop($lines);
echo implode('<br />', $lines);
Cheers!
This is a more general answer - you can get any amount of lines using this:
function getLines($paragraph, $lines){
$lineArr = explode("\n",$paragraph);
$newParagraph = null;
if(count($lineArr) > 0){
for($i = 0; $i < $lines; $i++){
if(isset($lines[$i]))
$newParagraph .= $lines[$i];
else
break;
}
}
return $newParagraph;
}
you could use echo getLines($crow->content,2); to do what you want.

Categories