preg_replace: how do I strip off all white space and ?
Solution 1
I found another solution
$this->content = preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]", html_entity_decode($this->content));
Solution 2
There is no need for a regex based solution. You can simply use str_replace
as:
$input = "[b] bold [/b]";
$input = str_replace(array(' ',' '),'',$input);
echo trim($input); // prints [b]bold[/b]
Solution 3
$this->content = preg_replace(
'~\[(.*?)](?:\s| )*(.*?)(?:\s| )*\[/\\1]/',
'[$1]$2[/$1]',
$this->content
);
Solution 4
A little late to answer but hopefully might help someone else. The most important thing while extracting content from html is to use utf8_decode() in php. Then all other string operations become a breeze. Even foreign characters can be replaced by directly copy pasting characters from browser into the php code.
The following function replaces
with empty character. Then all white spaces are replaced with empty character using preg_replace()
.
function clean($str)
{
$str = utf8_decode($str);
$str = str_replace(" ", "", $str);
$str = preg_replace("/\s+/", "", $str);
return $str;
}
$html = "[b] bold [/b]";
$output = clean($html);
echo $output;
[b]bold[/b]
Solution 5
Another method that would work is:
$this->content = trim(str_replace(' ','',$this->content));
PHP Manual links:
trim() http://us.php.net/trim
*note: This is assuming $this->content contains only the string posted by OP
Comments
-
Run almost 4 years
how do I strip off all white space and
?I have this as a input in a wrapper I create,
[b] bold [/b]
so before turning the text to bold, i want to strip off all white spaces and  , and turn it into
[b]bold[/b]
,$this->content = preg_replace("/\[(.*?)\]\s\s+(.*?)\s\s+\[\/(.*?)\]/", "[$1]$2[/$3]", $this->content);
but it does not work! can you help please?
-
poke almost 14 yearsThe problem is simply that php doesn't recognize
as a whitespace character, simply because it is infact just a string of 6 characters. If you want to use regexps for that, you'll need to tell php to explicitely match
as well. -
Your Common Sense almost 14 yearswhere do you get these
? may be not to add it at all?
-
-
NikiC almost 14 yearsThis will remove the whitespace outside the
[b]
, too. But I don't know whether that's expected behavior or not. -
poke almost 14 yearsThat wouldn't work either. The original question didn't show the
characters that OP wants to be removed. Also your solution will not work at all, since the square brackets are not escaped, and all your grouping doesn't help either. -
codaddict almost 14 yearsLooking at his sample input/output looks like thats what he wants.
-
poke almost 14 yearsExcept that the sample doesn't show text outside of the tags.
-
NikiC almost 14 years@poke: I don't get what you want to say. The closing square bracket
]
doesn't need to be escaped. All I did was take the OPs regex, remove unnecessary parts and add recognition of
. -
poke almost 14 years@nikic: Huh? Did you change your answer in between?
-
NikiC almost 14 years@poke: I think you are referring to another answer. I didn't change this one.
-
poke almost 14 years@nikic: o.O Yeah I think so, my bad then.. confused - Could you slightly change your answer (add a space or something) so I can remove the -1 again?
-
Vladd almost 10 yearsAnd what if get one more in next string he is parsing? This is very bad answer