Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我想删除HTML中标记之间的空格_Php_Regex - Fatal编程技术网

Php 我想删除HTML中标记之间的空格

Php 我想删除HTML中标记之间的空格,php,regex,Php,Regex,$HTML包含HTML代码。如果它在>\s{1,}\对于空格,请使用: 仅对于删除后div空间,用户 $HTML = '<div calss="something"> </div> <div> here</div>'; $string = str_replace('> ', '>', $HTML); $HTML='here'; $string=str_replace('>','>',$HTML); It's removing only

$HTML
包含HTML代码。如果它在
>
之间有一些空格,则可以通过
<和>
修剪foreach循环中的数组项进行分解,并通过
<和>
进行内爆

$html = "<div> </div> <div> here</div> <p>     text</p>";

function trim_html($html)
{
    $elements = explode("<", $html);
    $result = array();

    foreach($elements as $element)
    {
        array_push($result, trim($element));
    }

    $result = implode("<", $result);
    $elements = explode(">", $result);
    $result = array();

    foreach($elements as $element)
    {
        array_push($result, trim($element));
    }

    $result = implode(">", $result);

    return $result;
}
$html=“此处text

”; 函数trim_html($html) { $elements=分解(“,$result); 返回$result; }

原始的工作理念,想投票否决吗?不客气)

$HTML=preg\u replace('/>+我想这正是你想要的

$str='This is a sentence> <and we wish to remove> <spaces between> <thingies';
$corrected=preg_replace('@\>\s{1,}\<@','><',$str);

echo '<textarea style=\'width:800px;height:400px;\'>',$corrected,'</textarea>';
$str='这是一个句子>\s{1,}\对于空格,请使用:

仅对于删除后div空间,用户

$HTML = '<div calss="something"> </div> <div> here</div>';
$string = str_replace('> ', '>', $HTML);
$HTML='here';
$string=str_replace('>','>',$HTML);

It's removing only罚款,你的问题在哪里?预期的结果是什么?我在这里期望的只是空格,使用str_replace:$string=str_replace(“”,,$string);对于所有空格,使用preg_replace:$string=preg_replace(‘/\s+/’,‘,$string);我认为op不知道他想要什么!这不会删除>之间有2个或更多空格的字符串,有趣的是,低质量的答案通常以“Try”开头你没有提到任何数量的空格…@KarolyHorvath低质量的答案有效,这个问题是错误的!@KarolyHorvath实际上问题是
它有一个空格在>这将不仅仅是删除
>之间的空格检查这个新代码,它工作正常,删除了>之间的空格不删除(空格)text预计在堆栈溢出中,它会为提供的任何代码添加解释。此外,它与OP的尝试相同,只是
\s
被替换为空格。它会起作用,但它也会删除用户与此代码之间的空格,以满足您的新需求@Kuppuraj$string=str_replace(“>”,“>”,$HTML);如果你有
这里text

?:d删除所有空白的建议是荒谬的。最后一个有效的解决方案不是删除
div
后面的空格,而是删除任何html标记。。。
$HTML=preg_replace('/> +</','><',$HTML);
$str='This is a sentence> <and we wish to remove> <spaces between> <thingies';
$corrected=preg_replace('@\>\s{1,}\<@','><',$str);

echo '<textarea style=\'width:800px;height:400px;\'>',$corrected,'</textarea>';
$HTML = '<div> </div> <div> here</div>';

$string = str_replace(' ', '', $HTML);
$string = preg_replace('/\s+/', '', $HTML);
$HTML = '<div calss="something"> </div> <div> here</div>';
$string = str_replace('> ', '>', $HTML);