Remove information from string using php - php

I am trying to remove the following type of info from a string using php :
[url:2q57noz9]http://www.mysite.com/other/screencaps-from-ddd-t7099.html#p24174[/url:2q57noz9]
there are random numbers assigned to the [url: bit which makes it harder. I tried to adapt the following which works for image tags but I don't think it likes square brackets put in like I have. This is what I used for images :
$message = preg_replace(array("/<img[^>]+\>/i","/<!--[^>]+\-->/i"), "", $message);
and this is how I tried to modify it without success :
$message = preg_replace("/[[^>]+\]/i", "", $message);

add backslash before brackets
$message = preg_replace('/\[[^>]+\]/i', "", $message);
and use single Quotation for holding string

Related

How to remove \n\n characters, line break from php string

I am trying to remove these \n\n characters that appear in my string when I post a form.
My code is;
$text = $textdb['columnname'];
echo trim(preg_replace('/\s\s+/', ' ', $text));
But it doesn't work. I have tried literally every example here and still nothing works. Am I suppose to do something prior to this? Does this work with a certain version of php? Am using 5.6 by the way. Thanks.
Just use nl2br
Example:
$text = nl2br("hey\n neighbor");
would print
hey
neighbor

Remove Unicode Zero Width Space PHP

I have a text in Burmese language, UTF-8. I am using PHP to work with the text. At some point along the way, some ZWSPs have crept in and I would like to remove them. I have tried two different ways of removing the characters, and neither seems to work.
First I have tried to use:
$newBody = str_replace("​", "", $newBody);
to search for the HTML entity and remove it, as this is how it appears under Web Inspector. The spaces don't get removed. I have also tried it as:
$newBody = str_replace("&#8203", "", $newBody);
and get the same no result.
The second method I tried was found on this question Remove ZERO WIDTH NON-JOINER character from a string in PHP
which looked like this:
$newBody = str_replace("\xE2\x80\x8C", "", $newBody);
but I also got no result. The ZWSP was not removed.
An example word in the text ($newBody) looks like this : ယူ​​က​​ရိန်
And I want to make it look like this : ယူကရိန်း
Any ideas? Would a preg_replace work better somehow?
So I did try
$newBody = preg_replace("/\xE2\x80\x8B/", "", $newBody);
and it appears to be workings, but now there is another issue.
<a class="defined" title="Ukraine">ယူ​က​ရိန်း</a>
gets transformed into
<a class="defined _tt_t_" title="Ukraine" style="font-family: 'Masterpiece Uni Sans', TharLon, Myanmar3, Yunghkio, Padauk, Parabaik, 'WinUni Innwa', 'Win Uni Innwa', 'MyMyanmar Unicode', Panglong, 'Myanmar Sangam MN', 'Myanmar MN';">ယူကရိန်း</a>
I don't want it to add all that extra stuff. Any ideas why this is happening? Apart from coming up with some way to target only the text in between , is there another way to prevent the preg_replace from adding all this extra stuff? Btw, using google chrome on a mac. It seems to act a bit differently with firefox...
This:
$newBody = str_replace("​", "", $newBody);
presumes the text is HTML entity encoded. This:
$newBody = str_replace("\xE2\x80\x8C", "", $newBody);
should work if the offending characters are not encoded, but matches the wrong character (0xe2808c). To match the same character as #8203; you need 0xe2808b:
$newBody = str_replace("\xE2\x80\x8B", "", $newBody);

PHP preg_replace needs improvement

Ok, I have a string as follows:
$disallowedBBC = 'abbr|acronym|anchor|bdo|black|blue|br|color|email|flash|font|ftp|glow|green|html|hr|img|iurl|li|list|ltr|url|quote';
And than a preg_replace on the actual string ($message variable) that should get rid of all bbc code that is not allowed according to the $disallowedBBC variable:
$message = preg_replace("/\[($disallowedBBC)[^]]*](.*?)\[\/($disallowedBBC)\]/is", "$2", $message);
But, for some reason, the [hr] tag is getting past this preg_replace. So, in this case:
$message = '[hr]Test';
It outputs the [hr] tag, but should remove it. What is wrong with my regex?
Basically...
How to change it so that it removes all [hr] and/or [hr]Test[/hr] altogether? But would also need to get rid of instances where [url=http://someurl.com]Some Url[/url]. And it should remove [color=red] from a string as follows: [color=red]Testing
For example, it needs to get rid of [{tag}] and if it has a closing tag [/{tag}], but if there is no closing tag, it needs to get rid of the opening tag, and vice versa. It should be able to capture anything within the {tag} that is within the brackets as well, such as: [quote author=Solomon time=7834783470]Just a quote here[/quote] Additional text here...
So, this should output: Just a quote here Additional text here...
I think you need two preg_replaces. One to first get rid of the pairs of [hr]...[/hr] and then a second to get rid of any remaining [hr]...
$message = preg_replace("/\[($disallowedBBC)[^\]]*](.*?)\[\/($disallowedBBC)\]/is", "$2", $message);
$message = preg_replace("/\[($disallowedBBC)[^\]]*]/is", "", $message);
I you try to do it in one step, then things like "abc[br]blahblah[hr]gak[/hr]def" will become "abcdef". You might be able to do it if you can place restrictions on the blahblah portion.
You can, of course, combine these into one preg_replace call by using the array syntax (but remember that the order matters):
$patterns = array("/\[($disallowedBBC)[^\]]*](.*?)\[\/($disallowedBBC)\]/is",
"/\[($disallowedBBC)[^\]]*]/is", );
$replacements = array("$2", "");
$message = preg_replace($patterns, $replacements, $message);

str_replace/nl2br not acting as expected

I'm having some weird difficulty with removing "\n"'s from data that is pulled from a database.
The data is an email and gets stored with \n's throughout it by the system that inputs it.
When I display it, I've tried to remove these \n's using the following:
$htmlbody = str_replace("\n", "", $message['htmlbody']);
or
nl2br($message['htmlbody']);
but both commands still return a string that is full of \n's.
The variable $message['htmlbody'] includes a string like \n\n <div>\n Example Data \n </div>\n, and this data remains the same after being passed through str_replace.
The data originally comes from a JSON webhook which has replaced all the newlines in an HTML email with \n's. I have control over the data being put into the database as well, and have tried using the above actions on the original data with the same result.
Any thoughts on what might cause this?
Cheers.
Found this in a google search
$message = preg_replace("/\n|\r/", " ", $message['htmlbody']);
Which will replace newlines with whitespace.
to replace first \r\n then \r and then \n use this:
$message['htmlbody'] = preg_replace(array("/\r\n/", "/\r/", "/\n/"), array(' ',' ', ' '), $message['htmlbody']);
$message['htmlbody'] = str_replace("\n", "", $message['htmlbody']);
If the string you try to modify have \n displayed, and not interpreted as a new line, use :
$message['htmlbody'] = str_replace("\\n", "", $message['htmlbody']);
Try passing $message through stripslashes. It is possible that you have \\n\\r, that is double slashes which prevent correct escaping...
$message['htmlbody'] = nl2br(stripslashes($message['htmlbody']))

PHP str_replace not working correctly

I'm using str_replace and it's not working correctly.
I have a text area, which input is sent with a form. When the data is received by the server, I want to change the new lines to ",".
$teams = $_GET["teams"];
$teams = str_replace("\n",",",$teams);
echo $teams;
Strangely, I receive the following result
Chelsea
,real
,Barcelona
instead of Chealsea,real,Barcelona.
What's wrong?
To expand on Waage's response, you could use an array to replace both sets of characters
$teams = str_replace(array("\r\n", "\n"),",",$teams);
echo $teams;
This should handle both items properly, as a single \n is valid and would not get caught if you were just replacing \r\n
Try replacing "\r\n" instead of just "\n"
I would trim the text and replace all consecutive CR/LF characters with a comma:
$text = preg_replace('/[\r\n]+/', ',', trim($text))
I had the same issue but found a different answer so thought I would share in case it helps someone.
The problem I had was that I wanted to replace \n with <br/> for printing in HTML. The simple change I had to make was to escape the backslash in str_replace("\n","<br>",($text)) like this:
str_replace("\\n","<br>",($text))

Categories