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?
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]
I found another solution
$this->content = preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]", html_entity_decode($this->content));
$this->content = preg_replace(
'~\[(.*?)](?:\s| )*(.*?)(?:\s| )*\[/\\1]/',
'[$1]$2[/$1]',
$this->content
);
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]
You can just replace the spaces with empty strings, e.g.
preg_replace("/(?:\s| )+/", "", $this->content, -1)
The -1 causes the replace to hit every instance of the match.
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
To give you a complete solution with regular expressions as well:
$this->content = preg_replace( '/\[([a-z]+)\](?: |\s)*(.*?)(?: |\s)*\[\/([a-z]+)\]/', '[$1]$2[/$3]', $this->content );
But in this case you should rather combine the whitespace removal and bbcode transformation to make sure that the tags are correctly:
$this->content = preg_replace( '/\[b\](?: |\s)*(.*?)(?: |\s)*\[\/b]/', '<b>$2</b>', $this->content );
精彩评论