标签: str-replace

php str_replace问题

奇怪的是,我一定是在遗漏一些东西.

$city = "vancouver";

$insert1 = "https://www.site.ca/buy/vancouver/28130965/";    

$url2 = str_replace('/$city/','index.php?deal=',$insert1);
Run Code Online (Sandbox Code Playgroud)

https://www.site.ca/buy/vancouver/28130965/返回?

php str-replace

2
推荐指数
1
解决办法
268
查看次数

PHP str_replace是否有超过13个字符的限制?

直到第13个字符被击中为止.一旦str_ireplace在cyper数组中命中"a",str_ireplace就会停止工作.

数组的大小是否有限制?请记住,如果键入"abgf",我会得到"nots",但如果我输入"abgrf",当我得到"注释"时,我会得到"notrs".我的大脑无法弄明白.

$_cypher = array("n","o","p","q","r","s","t","u","v","w","x","y","z","a","b","c","d","e","f","g","h","i","j","k","l","m");

$_needle = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");


$_decryptedText = str_ireplace($_cypher, $_needle, $_text);
echo $_decryptedText;
Run Code Online (Sandbox Code Playgroud)

救命?

php compiler-errors str-replace

2
推荐指数
1
解决办法
1079
查看次数

如何使用不会更新的新字母替换字符串中的字母

让我们说我有一些代码:

$text = $_POST['secret'];

$replaces = array(
        'a' => 's',
        'b' => 'n',
        'c' => 'v',
        'd' => 'f',
        'e' => 'r',
        'f' => 'g',
        'g' => 'h',
        'h' => 'j',
        'i' => 'o',
        'j' => 'k',
        'k' => 'l',
        'l' => 'a',
        'm' => 'z',
        'n' => 'm',
        'o' => 'p',
        'p' => 'q',
        'q' => 'w',
        'r' => 't',
        's' => 'd',
        't' => 'y',
        'u' => 'i',
        'v' => 'b',
        'w' => 'e',
        'x' => 'c',
        'y' …
Run Code Online (Sandbox Code Playgroud)

php string str-replace

2
推荐指数
1
解决办法
2495
查看次数

使用str_replace时将数组转换为字符串的注意事项

当我str_replace将数组用作第二个参数时,例如对此问题的广泛支持和接受的答案中,我得到了数组到字符串转换的通知。为什么?例:

$str = 'a b a ba a';
$numerals = range(1, 10);
$str = str_replace('a', $numerals, $str);
Run Code Online (Sandbox Code Playgroud)

给出:

PHP注意:第1行上的PHP Shell代码中的数组到字符串的转换

和以下输出:

阵列b阵列b阵列

代替

1 b 2 b3 4

php str-replace

2
推荐指数
1
解决办法
3866
查看次数

PHP的str_replace替代品

在PHP str_replace 手册中,它说明如下:

因为str_replace()从左到右替换,所以在执行多次替换时,它可能会替换先前插入的值.另请参阅本文档中的示例.

是否有一个没有这个问题的等效功能或我怎样才能安全地做到这一点?

php str-replace

2
推荐指数
1
解决办法
4064
查看次数

将_blank添加到所有外部链接

可能重复:
抓取A元素的href属性
解析包含"href"标记中特定单词的所有链接

我正在使用以下函数将_blank添加到我网站上的所有链接.

function targetBlank($text) {
  $return = str_replace('<a', '<a target="_blank"', $text);
  return $return;
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种解决方案,只在外部链接(不在我的域上)而不是所有链接上应用此功能.

php uri str-replace

2
推荐指数
1
解决办法
3167
查看次数

使用Python替换多个字符的字符

我一直在尝试使用Python解决以下问题,到目前为止没有成功:

假设你有一个字符串'0','1'和'?'.'?' 符号可以是'0'或'1'.您的目标是打印此类给定字符串的所有可能输出.例如,字符串'0?1?'的输出 应该是'0010','0011','0110'和'0111'

我尝试过以下方法:

def comb(S):

    if not '?' in S:
        yield S
    else:
        yield comb(S.replace('?','0',1))
        yield comb(S.replace('?','1',1))             

S = '0?1??011'
S_generator = comb(S)
for s in  S_generator:
    print s
Run Code Online (Sandbox Code Playgroud)

结果很奇怪,而不是我想要得到的:

<generator object comb at 0x106b2ceb0>
<generator object comb at 0x106b2cf00>
Run Code Online (Sandbox Code Playgroud)

知道为什么它不工作,以及我应该如何更改代码才能工作?

python string generator str-replace

2
推荐指数
1
解决办法
488
查看次数

PHP用数组中的值替换子字符串

我有一个文字说:

$text = "An Elephant is an Elephant but an Elephant is not an Elephant"
Run Code Online (Sandbox Code Playgroud)

我有一个阵列说:

$array = array("First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eight", "Ninth");
Run Code Online (Sandbox Code Playgroud)

在文中你可以看到有很多出现的"大象".我想要做的是我想用数组中的唯一值替换Elephant的出现,结果应该是这样的:

$result = "An Fifth is an Seventh but an First is not an Fourth"
Run Code Online (Sandbox Code Playgroud)

到目前为止我试过这个:

$arr = array("First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eight", "Ninth");
$text = "an elephant is an elephant but an elephant is not an elephant";
$array = explode(" ", $text);
$new_arr = array_diff($array, array("elephant"));
$text = implode(" …
Run Code Online (Sandbox Code Playgroud)

php arrays string str-replace

2
推荐指数
1
解决办法
78
查看次数

如果不存在,如何将.jpg添加到字符串的结尾

我有"原始列表",我正在尝试使用php将其转换为"所需列表".当.jpg出现在字符串的末尾时,我想什么都不做.如果.jpg不是字符串的结尾,我想在最后添加.jpg.存储在$ badPhotos中的"原始列表"为一个长字符串.

   Original List                                      Desired List
http://i.imgur.com/4ReyOhl                    http://i.imgur.com/4ReyOh1.jpg
http://i.imgur.com/4ReyOh2.jpg                http://i.imgur.com/4ReyOh2.jpg
http://i.imgur.com/4ReyOh3                    http://i.imgur.com/4ReyOh3.jpg
http://i.imgur.com/4ReyOh4                    http://i.imgur.com/4ReyOh4.jpg
http://i.imgur.com/4ReyOh5.jpg                http://i.imgur.com/4ReyOh5.jpg
Run Code Online (Sandbox Code Playgroud)

我一直在使用以下内容修改字符串,如果有人添加了a,其中a.属于,但现在我只需要在字符串不以.jpg结尾时修改sting.

$badPhotos = str_replace(',', '.', $badPhotos);
Run Code Online (Sandbox Code Playgroud)

想法?谢谢!

php string if-statement str-replace

2
推荐指数
1
解决办法
568
查看次数

如何在条件中使用str_replace

我想在某些条件下使用str_replace.我正在开发一个应用程序,它从文本区域输入一个文本块并将其输出为1行.只要满足" end of line"或" space + with end of line",字符串就会替换为<br>

我现在已经找到了解决方案,无论何时end of line满足,字符串都被替换为<br>.但是如果用户输入一个space之前end of line,我需要在更换EOL之前摆脱那个空间<br>.

我的代码

$refresheddata = str_replace("\n", '<br>', $data);
Run Code Online (Sandbox Code Playgroud)

样本输入

This is the first line with a space at the end 
This is the second line which donot have a space at the end
Run Code Online (Sandbox Code Playgroud)

输出我的代码

This is the first line with a space at the end <br>This is the second line which donot have a …
Run Code Online (Sandbox Code Playgroud)

html php str-replace

2
推荐指数
1
解决办法
430
查看次数