如何在php中的特定单词后替换所有内容

时间:2015-01-16 12:29:12

标签: php

$url =file("list.txt");
foreach ($url as $sites) { 
    $sites = trim($sites);
    echo $sites . " </ br>";
}

list.txt 包含一些网址

http://example.com/cms/wp-content/themes/
http://example.com/wp-content/plugins/
http://example.com/wp-content/themes/Avada-Child-Theme/

我怎么能删除“/ wp-content /”这个词以及之后的所有内容 是

http://example.com/cms
http://example.com
http://example.com

6 个答案:

答案 0 :(得分:3)

查看http://docs.php.net/strstr

处的参数$before_needle
$o = strstr($url, '/wp-content/', true);

答案 1 :(得分:1)

如何使用preg_replace

类似的东西:

$sites = trim(preg_replace( '#/wp-content.*#', '', $sites));

答案 2 :(得分:0)

$lines = file('list.txt');
$find = '/wp-content/';
foreach ($lines as $line) { 
    $line = trim($line);
    $pos = strpos($line, $find);
    if($pos !== false) {
        echo substr($line, 0, $pos) . '<br>';
    } else {
        echo 'Not found ' . $find . '<br>';
    }
}

答案 3 :(得分:0)

首先按新行爆炸你的内容,然后遍历每个内容并使用substr函数删除匹配。以下功能对我有用:

<?php 
// can remove variables from: full url, from urls related to site root, form just a query string like "a=1&b=2"
if(!function_exists("remove_var_from_url")){
    function remove_var_from_url($variable_name, $url_string){

        // this is anything before the "?" sign
        $base_url = '';
        // the variable separator, can be "?" if is a full URL or can be empty, if we just have "&sort=sales&oprder=asc"
        $separator = "";
        $start_pos = 0;
        $return_string = "";
        //
        if(strpos($url_string,"?")!==false){
            $start_pos = strpos($url_string, "?")+1;
            $separator = "?";
            $base_url = substr($url_string, 0, $start_pos-1);
        }
        // start building the string from the base url (which can be empty)
        $return_string = $base_url;
        $url_vars_string = substr($url_string, $start_pos);
        $names_and_values = explode("&", $url_vars_string);

        //
        foreach($names_and_values as $value){
            list($var_name, $var_value) = explode("=", $value);
            if($var_name != $variable_name){
                // add the "?" once if needed
                if(!$separator_added){
                    $return_string.= $separator;
                    $separator_added = true;
                } else {
                    $return_string.= "&";
                }
                $return_string.= $var_name."=".$var_value;
            }
        }

        // remove "&" from margins
        $return_string = trim($return_string, "&");

        // remove the "?" if is at the end, it means it was just one variable that was removed
        $return_string = rtrim($return_string, "?");

        return $return_string;
    }
}
?>

答案 4 :(得分:0)

这应该有效:

<?php

    $url =file("list.txt");
    foreach ($url as $sites) {
        $sites = trim($sites);
        $pos = strpos($sites, 'wp-content');
         $newStr = substr($sites,0,$pos );
        echo $newStr . " </ br>";
    }
?>

答案 5 :(得分:0)

我建议您先在每个字符串上应用strpos。 Strpos将返回首次出现字符串的位置。然后使用substr来获取该字符串之前的所有内容。 `$ lines = file(&#39; list.txt&#39;);     $ find =&#39; / wp-content /&#39 ;;     foreach($行为$ line){     $ position = strpos($ line,&#39; / wp-content&#39;);

if($position)
$string = substr($line, 0, $position); 
}`