regex - remove empty lines at the end - php

I want to remove empty quote lines at the end of a mail:
> Hello
>
> thats quotet>
>
>
to
> Hello
>
> thats quotet>
But the regex preg_replace("/^>\W*$/m", "", $input_lines) won't do the trick because it also removes the > of the empty line thats not at the end.
edit: And there is still the problem, that only > at the beginning of a line should be removed

Use rtrim, it removes specified characters from the right side of the string. We want to remove ">" and new line characters from the end.
Code:
echo rtrim($input, ">".PHP_EOL);
Result:
> Hello
>
> thats quotet

You can use:
$result = preg_replace('~^(?:^>\h*\R?)*+\z~m', '', $input_lines);
To avoid useless tests you can also try this more hand-driven pattern:
$result = preg_replace('~^(?:^>\h*\R?)*+(*SKIP)\z~m', '', $input_lines);

Related

Remove new line in wysiwyg redactor

I'm using the wysiwyg in the back end and use the following code to limit the string, "...read more" will be displayed at the end of the sentence.
function limit_words($string)
{
$word_limit = '60';
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
<?php echo limit_words($rows['content']); ?>... read more
Here is my problem. The "...read more" is showing properly when the string characters are more than 60.
But if the string characters are less than 60, the read more text is showing in new line. I know this is causing by <p>, which is auto generate from wysiwyg.
Is it possible to solve this issue on this section only?
Solved by using regular expression to remove <p> tags around elements
$replaced = preg_replace('~</?p[^>]*>~', ' ', $rows['content']);
echo limit_words($replaced); ?>... read more

How to remove part of string in php?

I have this string
NOTEBOOK > ABC
TABLET > DFG
I want to remove everything after '>' including '>'
I tried this
$category = substr($categoryGet,0,strrpos($categoryGet.">",">"));
No result so far
You can use preg_replace
$category = preg_replace('/>[^>]*$/m', '', $category);
The regular expression matches > followed by any non-> characters until the end of the line. The m modifier makes $ match the end of each line in the string.
$str="NOTEBOOK > ABC
TABLET > DFG";
$x=explode("\n",$str); //break by lines
foreach($x as $y){
$k=explode('>',$y);
echo trim($k[0]); //or that ever form you want the output to be
}

How to remove < and > symbol?

I have a text name #chatfun <chinu,25,M,123456> i want to #chatfun chinu,25,M,123456 How to replace < and > tag.
My Code :
<?php
$text = '#chatfun <chinu,25,M,123456>';
echo strip_tags($text);
?>
Above code i am getting the blank text. How to get my actual result?
Just use str_replace with the angular brackets in an array.
echo str_replace(array('>', '<'), '', $text);
Another option is to use regex with preg_replace
echo preg_replace("/[<>]+/", '', $text);
Have you tried str_replace?
$text = str_replace(array('<', '>'), '', '#chatfun <chinu,25,m,123456>');
That will replace the unwanted characters with nothing. Only caveat is that if the characters show up anywhere else, they, too, will be replaced.
<?php
$to_remove = array("<",">");
$text = '#chatfun <chinu,25,M,123456>';
echo str_replace($to_remove,"",$text);
?>
See refrence
You're getting blank text because the text between the < and > is considered part of a tag.
Use str_replace(array('<','>'),'',$text)

preg_replace between > and <

I have this:
<div> 16</div>
and I want this:
<div><span>16</span></div>
Currently, this is the only way I can make it work:
preg_replace("/(\D)(16)(\D)/", "$1<span>$2</span>$3", "<div> 16</div>")
If I leave off the $3, I get:
<div><span>16</span>/div>
Not quite sure what your after, but the following is more generic:
$value = "<div> 16 </div>";
echo(preg_replace('%(<(\D+)[^>]*>)\s*([^\s]*)\s*(</\2>)%', '\1<span>\3</span>\4', $value));
Which would result in:
<div><span>16</span></div>
Even if the value were:
<p> 16 </div>
It would result in:
<p><span>16</span></p>
I think you meant to say you're using the following:
print preg_replace("/(\\D+)(16)(\\D+)/", "$1<span>$2</span>$3", "<div>16</div>");
There's nothing wrong with that. $3 is going to contain everything matched in the second (\D+) group. If you leave it off, obviously it's not going to magically appear.
Note that your code in the question had some errors:
You need to escape your \'s in a string.
You need to use \D+ to match multiple characters.
You have a space before 16 in your string, but you're not taking this into account in your regex. I removed the space, but if you want to allow for it you should use \s* to match any number of whitespace characters.
The order of your parameters was incorrect.
Try following -
$str = "<div class=\"number\"> 16</div>";
$formatted_str = preg_replace("/(<div\b[^>]*>)(.*?)<\/div>/i", "$1<span>$2</span></div>", $s);
echo $formatted_str;
This is what ended up working the best:
preg_replace('/(<.*>)\s*('. $page . ')\s*(<.*>)/i', "$1" . '<span class="curPage">' . "$2" . '</span>' . "$3", $pagination)
What I found was that I didn't know for sure what tags would precede or follow the page number.

str_replace just returns caps

i have a lil problem here..i'm using str_replace to replace the most common words..and for some reason its replacing every letter except caps.
for example..if i had the code below
$str ="Fat string of Text.";
$commonwords = array('fat','of','random');
$cleantext = str_replace($commonwords,'',$str);
echo $cleantext;
it would echo.. F T
any ideas what i did wrong..
thanks in advance
and oh..i tried str_ireplace.. but nothing
This echos "Fat string Text".
Your PHP installation may be wrong or your posted code that does not exactly match the program you are running
Also, str_ireplace echos "string Text".
Can't reproduce that on PHP 5.3.3. I get:
php > $str ="Fat string of Text.";
php > $commonwords = array('fat','of','random');
php > $cleantext = str_replace($commonwords,'',$str);
php > echo $cleantext;
Fat string Text.
php > $cleantext = str_ireplace($commonwords,'',$str);
php > echo $cleantext;
string Text.
as expected.

Categories