Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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微观优化)_Php_Regex_Micro Optimization - Fatal编程技术网

从一开始移除字符串最有效、最快的方法是什么?(PHP微观优化)

从一开始移除字符串最有效、最快的方法是什么?(PHP微观优化),php,regex,micro-optimization,Php,Regex,Micro Optimization,我有一个类似于www.host.com的字符串,我需要删除它的开头(在本例中是www.),使其成为host.com。有很多这样的开头(比如:m.,wap.等等)。如何快速有效地完成 目前我正在使用此代码,但我认为应该有更好/更快/更干净的方法: <?php function _without_start( $val, $start ) { if( _starts( $val, $start ) ) { $len = mb_strlen( $start );

我有一个类似于
www.host.com
的字符串,我需要删除它的开头(在本例中是
www.
),使其成为
host.com
。有很多这样的开头(比如:
m.
wap.
等等)。如何快速有效地完成

目前我正在使用此代码,但我认为应该有更好/更快/更干净的方法:

<?php

function _without_start( $val, $start )
{
    if( _starts( $val, $start ) )
    {
        $len = mb_strlen( $start );
        $val = mb_substr( $val, $len );
    }

    return $val;
}

function _starts( $str, $needle ) 
{
    return ( mb_substr( $str, 0, mb_strlen($needle) ) === $needle );
}

/********************************************/

$host = 'www.host.com';

$remove_from_beginning = array( 'www.', 'wap.', 'admin.' );
foreach( $remove_from_beginning as $start )
{
    $host = _without_start( $host, $start );
}

var_dump( $host );

从字符串中删除某些内容不需要foreach,这一个会更好

$url = preg_replace("/^(www|wap)\./si","","www.wap.com");
echo $url;

你们不需要foreach从字符串中删除一些东西,这个会更好

$url = preg_replace("/^(www|wap)\./si","","www.wap.com");
echo $url;
由于您添加了一个Regex标记,您可以在Regex中使用更改创建一个列表,并对照字符串检查它,并替换为
字符串

正则表达式:
^(www | admin | wap)\.

由于您添加了一个Regex标记,因此您可以在Regex中使用更改创建一个列表,并对照字符串进行检查,然后替换为
空的
字符串

正则表达式:
^(www | admin | wap)\.


使用
爆炸
在数组中

function _without_start($host, $prefixes) {
    list($prefix, $remainder) = explode('.', $host, 2);
    return in_array($prefix, $prefixes) ? $remainder : $host;
}

$prefixes = ['www', 'wap', 'admin'];
$host = 'www.host.com';

echo _without_start($host, $prefixes);

在数组中使用
分解

function _without_start($host, $prefixes) {
    list($prefix, $remainder) = explode('.', $host, 2);
    return in_array($prefix, $prefixes) ? $remainder : $host;
}

$prefixes = ['www', 'wap', 'admin'];
$host = 'www.host.com';

echo _without_start($host, $prefixes);

如果您想要一个基于
regex
的解决方案(正如标签中提到的
regex
),那就来吧

$remove_from_beginning = array( 'www.', 'wap.', 'admin.' );
$final = "";

foreach( $remove_from_beginning as $start )
{
    $final = $final . "" . substr_replace($start, '', -1) . "" . "\.|";
}

$final = substr_replace($final, '', -1);

echo preg_replace("/" . "" . "(?:" . "" . $final . "" . ")" . "" . "(.*)" . "" . "/", "$1", "www.exaple.com");

如果您想要一个基于
regex
的解决方案(正如标签中提到的
regex
),那就来吧

$remove_from_beginning = array( 'www.', 'wap.', 'admin.' );
$final = "";

foreach( $remove_from_beginning as $start )
{
    $final = $final . "" . substr_replace($start, '', -1) . "" . "\.|";
}

$final = substr_replace($final, '', -1);

echo preg_replace("/" . "" . "(?:" . "" . $final . "" . ")" . "" . "(.*)" . "" . "/", "$1", "www.exaple.com");

在数组中使用
分解
而不是正则表达式。在数组中使用
分解
而不是正则表达式。