Jump to content

Recommended Posts

Hi all,

I have a function to covert white space into non-breaking white space...however, it also replaces white space in html tags. Is there a way to avoid this? I've been trying to figure it out for hours...can't quite manage it.

 

This is my code thus far:

function parse_code_ws($code)
{
$ws_code_parse = array(
'  '  => '  ',
'	' => '         '
);

return nl2br(str_replace(array_keys($ws_code_parse), $ws_code_parse, $code));
}

 

However, if this were to find <a    href="something">somethign</a> It would convert it to <a    href="something">somethign</a> which breaks the tag.

 

I was wondering if anyone knew how to either, remove white space from html tags, or just ignore the html tags altogether. I guess the second option would be better.

 

Thanks in advace :)

Link to comment
https://forums.phpfreaks.com/topic/161220-solved-html-tags/
Share on other sites

Well, you wouldn't want to remove white spaces from html tags, as this would be bad...ignoring them would definitely be better.

Perhaps something along these lines?

 

Example:

$code = <<<HTML
This is some text, and <a href="http://www.somesite.com/whatever.php">this link</a> is useless    !
Testing more text...
HTML;

$pattern = '#(^|>)[^<]+#';
function parse_code_ws($a){
$ws_code_parse = array(' '=>'  ', '    '=>'        '); // the second array key contains 4 consecutive spaces... change this to tabs or whatever it is supposed to be
return nl2br(str_replace(array_keys($ws_code_parse), array_values($ws_code_parse), $a[0]));
}
$code = preg_replace_callback($pattern, 'parse_code_ws', $code);
echo $code;

 

Output (via right-click view source):

This  is  some  text,  and  <a href="http://www.somesite.com/whatever.php">this  link</a>  is  useless        !<br /> 
Testing  more  text...

 

Granted, you have a space before   in your array values.. so I kept them there too.. not sure if you want those spaces there or not.. if not, simply remove them.

Link to comment
https://forums.phpfreaks.com/topic/161220-solved-html-tags/#findComment-850715
Share on other sites

Oooh nice.

 

However, I was hoping to do this with just pure regex rather than a callback...I already have a rather large array of stuff to convert (it's like a bbcode class) and I was hoping to just have the one array to prevent things getting confusing...

 

I've been messing around, and this avoids the html tags...

<?php
$str = 'hello  space <a  href="http://google">no space!!</a>';

echo preg_replace('/(?!\<.*?)  (?!.*?\>)/', '  ', $str);
?>

However, it also avoids the normal space as well :P

 

Surely there must be a way to do this with just regex?

 

Thanks for the reply,

Jack.

Link to comment
https://forums.phpfreaks.com/topic/161220-solved-html-tags/#findComment-850729
Share on other sites

I mean no offense by this, but I don't understand why you are so hooked up on pure regex solutions... (don't get me wrong, I personally love the stuff as well). But PHP offers such a wide array (no pun intended) of useful tools.. It almost feels as though you have fallen in love with say a power drill and want to use that for EVERYTHING, including cutting, hammering and plastering.

 

Use other tools that PHP offers..it's perfectly ok to mix regex with non regex (or even use no regex at all). The sooner you realise this and explorer other alternatives (or mix alternative solutions), the better off you'll be. Programming offers different ways in problem solving.. some solutions are better than others. Don't be afraid to explorer hybrid solutions (sometimes, pure regex just won't be good enough).. you'll learn a lot doing so, instead of being insistent on one tool only.

 

 

Link to comment
https://forums.phpfreaks.com/topic/161220-solved-html-tags/#findComment-850734
Share on other sites

Yeah...I know.  I just like things to be neat :P

 

Managed to contain it all in one function though!!

function parse_code_ws($code)
{
if(!is_array($code))
{
	$code = preg_replace_callback('/(^|>)[^<]+/', array($this, 'parse_code_ws'), nl2br($code));

	return $code;
}

$ws_code_parse = array(
'  '  => '  ',
'	' => '         '
);

return str_replace(array_keys($ws_code_parse), $ws_code_parse, reset($code));
}

I just have to make sure I don't accidentally give it an array to start off with otherwise it'll be in an infinite loop...:D I just crashed apache testing it out...

 

Nice one - I envy your regex knowledge.

Link to comment
https://forums.phpfreaks.com/topic/161220-solved-html-tags/#findComment-850742
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.