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


how to replace everything after specific word in php

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

列表.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

看看参数$before_needle在 http://docs.php.net/strstr

$o = strstr($url, '/wp-content/', true);

使用preg_replace怎么样?

像这样:

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

这应该有效:

<?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>";
    }
?>
$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>';
    }
}

首先通过新行分解您的内容,然后遍历每个内容并使用 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;
    }
}
?>

我宁愿建议你先在每个字符串上应用 strpos。Strpos 将返回字符串首次出现的位置。然后使用 substr 获取该字符串之前的所有内容。' $lines = 文件('列表.txt'); $find = '/wp-content/'; foreach ($lines as $line) { $position = strpos($line, '/wp-content');

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