标签: str-replace

如何用PHP替换字符串中的多个%标签%

替换PHP字符串中的一组短标签的最佳方法是什么,例如:

$return = "Hello %name%, thank you for your interest in the %product_name%.  %representative_name% will contact you shortly!";
Run Code Online (Sandbox Code Playgroud)

我将定义%name%是某个字符串,来自数组或对象,例如:

$object->name;
$object->product_name;
Run Code Online (Sandbox Code Playgroud)

等等..

我知道我可以在字符串上多次运行str_replace,但我想知道是否有更好的方法.

谢谢.

php string str-replace

3
推荐指数
1
解决办法
3605
查看次数

使用PHP格式化URL - http://www.google.com到www.google.com/; 还有str_replace()的问题

我在执行以下操作时遇到了一些麻烦..

http://www.google.com - > www.google.com/
https://google.com - > www.google.com/
google.com - > www.google.com/

我试图删除https:// or http://,确保将www.其添加到URL的开头,然后如果URL不存在则向URL添加尾部斜杠.

感觉就像我已经把这个中的大部分弄清楚但是我无法开始str_replace()工作我是怎么想的.

根据我的理解,这是如何使用str_replace:

$string = 'Hello friends';
str_replace('friends', 'enemies', $string);
echo $string;
// outputs 'Hello enemies' on the page
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止:

$url = 'http://www.google.com';

echo reformat_url($url);

function reformat_url($url) {
    if ( substr( $url, 0, 7 ) == 'http://' || substr( $url, 0, 8 ) == 'https://' ) { // if http:// or https:// is at the beginning of …
Run Code Online (Sandbox Code Playgroud)

php url substr str-replace

3
推荐指数
1
解决办法
2861
查看次数

替换所有类型的破折号

我有一个 Excel 文档,我使用库将其导入到 MySQL 中。

\n\n

但是文档中的一些文本包含破折号,我认为我已经替换了这些破折号,但显然不是全部。\n -\xe2\x80\x93-< - 所有这些都是不同的。

\n\n

有什么办法可以用这个替换所有类型的 dahes-

\n\n

主要问题是我不知道计算机中存在的所有破折号。

\n

php str-replace

3
推荐指数
1
解决办法
1042
查看次数

PHP强制str_replace只替换其中一个结果

这完美地运作:

$replaceLyrics=str_replace
(
    'story',
    '_____',
    'The story of my life I take her home I drive all night to keep her warm and time Is frozen (the story of, the story of)'
);

echo $replaceLyrics;
Run Code Online (Sandbox Code Playgroud)

输出:

The _____ of my life I take her home I drive all night to keep her warm and time Is frozen (the _____ of, the _____ of)
Run Code Online (Sandbox Code Playgroud)

我的问题是,是否可以使用内部php功能来仅替换匹配中的第一个匹配而不是全部替换.

我期待得到这个输出:

The _____ of my life I take her home I drive all night to keep …
Run Code Online (Sandbox Code Playgroud)

php arrays string foreach str-replace

3
推荐指数
1
解决办法
589
查看次数

str_replace()不会用重音替换字符

我有一个非常长的html字符串,其中包含希腊语和英语文本,其中大多数单词都包含html标签.我已经成功地使用下面的函数来删除文本中的希腊语重音符,以便它可以很好地大写.

但是在一些字符之后的本文中,替换停止发生,文本返回重音符号.我已经尝试了一些其他功能,我在网络上找到但没有运气.

有帮助吗?

我正在使用的功能是:

public static function stripAccents($str) {
    $a = array('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?');
    $b = array('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?');
    return str_replace($a, $b, $str);
}
Run Code Online (Sandbox Code Playgroud)

并且文本的示例是:

<div class="menu-page">

<h3>DESSERTS / ?????????</h3>

<h4>GOAT CHEESE ICE CREAM (N)</h4>
<p class="price">€7,50</p>
<p>??????? ???? ??????????? ????? ?? ?????????? ????????? & ?????? ?????????</p>
<p>With tomato marmalade & lemon cream</p>

<div class="clearfix"></div>

<h4>MOCHA A LA PALAIA</h4>
<p class="price">€8,50</p>
<p>??????? ?????, ????????? & ???????? ????????</p>
<p>Coffee and vanilla ice cream & Italian meringue<p>

<div class="clearfix"></div>

<h4>THE TRAIL OF CHOCOLATE (N)</h4>
<p class="price">€9,50</p> …
Run Code Online (Sandbox Code Playgroud)

html php string str-replace

3
推荐指数
1
解决办法
834
查看次数

为什么我不能直接将函数传递给replace方法?

我简直无法理解为什么第二行和第三行的输出彼此不同:

alphabet_ASCII = '';
for (i=65; i<=90; i++) {
  alphabet_ASCII += i;
}
alphabet_ASCII += '<br>';

document.body.innerHTML += alphabet_ASCII;



document.body.innerHTML += 
  alphabet_ASCII.replace(/\d{2}/g, x=>String.fromCharCode(x));

document.body.innerHTML += 
  alphabet_ASCII.replace(/\d{2}/g, String.fromCharCode);
Run Code Online (Sandbox Code Playgroud)

x=>String.fromCharCode(x)和之间有什么区别String.fromCharCode

javascript regex str-replace

3
推荐指数
1
解决办法
114
查看次数

许多列中的 str_replace 出现问题

我是 R 菜鸟。

\n\n

我正在处理大约 150 行和 21 列的数据框。我想将第 5 列到第 20 列的字符“-”更改为“0.00”。

\n\n

我正在使用这段代码,它可以单独工作:

\n\n
datos$max52sem<-str_replace(datos$max52sem,"-","0.00")\ndatos$min52sem<-str_replace(datos$min52sem,"-","0.00")\n
Run Code Online (Sandbox Code Playgroud)\n\n

我尝试使用“for”从所有列中更改它,而不是将所有变量名称写入 15 次。

\n\n

这就是我正在写的:

\n\n
mis_vars<-c("max52sem","min52sem","cierre_prev","cierre_hoy","max_hoy","min_hoy","ret_hoy","ret_sem","ret_mes","ret_a\xc3\xb1o","ret_ytd","vol","upa","vla","pvla","pu")\nfor(x in mis_vars)\n  datos$x<-str_replace(datos$x,"-","0")\n
Run Code Online (Sandbox Code Playgroud)\n\n

“mis_vars”是我想在数据框中更改的列(变量)的名称,但我从 R 得到这个答案,我不知道我做错了什么。

\n\n
\n

$<-.data.frame( *tmp*, "x", value = character(0))中的错误:\n 替换有 0 行,数据有 1220>

\n
\n

replace for-loop r str-replace dataframe

3
推荐指数
1
解决办法
2761
查看次数

Python Pandas - 'DataFrame' 对象没有属性 'str' - .str.replace 错误

我正在尝试将 panda 数据框中的 80 列替换为“”。我创建了一个要迭代的标头列表:

headers = ['h1', 'h2', 'h3'... 'h80']
Run Code Online (Sandbox Code Playgroud)

然后我使用标题列表来替换多列字符串值,如下所示:

dataFrame[headers] = dataFrame[headers].str.replace(',','')
Run Code Online (Sandbox Code Playgroud)

这给了我这个错误: AttributeError: 'DataFrame' object has no attribute 'str' 当我仅在一个标头上尝试相同的操作时,它效果很好,并且我需要使用“str.replace”,因为唯一的“replace”方法确实遗憾的是没有取代“,”。

谢谢

python str-replace pandas

3
推荐指数
1
解决办法
9852
查看次数

一次替换多个字符

有没有办法一次替换字符串中的多个字符,而不是这样做:

"foo_faa:fee,fii".replace("_", "").replace(":", "").replace(",", "")
Run Code Online (Sandbox Code Playgroud)

只是类似(与str.replace()

"foo_faa:fee,fii".replace(["_", ":", ","], "")
Run Code Online (Sandbox Code Playgroud)

python string replace str-replace

3
推荐指数
2
解决办法
3147
查看次数

使用C ++将一个字符串替换为另一个字符串

问题是我不知道输入字符串的长度。如果输入字符串为“ yyyy”,则只能替换我的函数。我认为解决方案是,首先,我们将尝试将输入字符串转换回“ yyyy”,并使用我的函数完成工作。

这是我的功能:

void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
{
    // Get the first occurrence
    size_t pos = data.find(toSearch);

    // Repeat till end is reached
    while( pos != std::string::npos)
    {
        // Replace this occurrence of Sub String
        data.replace(pos, toSearch.size(), replaceStr);
        // Get the next occurrence from the current position
        pos = data.find(toSearch, pos + replaceStr.size());
    }
}
Run Code Online (Sandbox Code Playgroud)

我的主要功能

std::string format = "yyyyyyyyyydddd";
findAndReplaceAll(format, "yyyy", "%Y");
findAndReplaceAll(format, "dd", "%d");
Run Code Online (Sandbox Code Playgroud)

我的预期输出应为:

%Y%d
Run Code Online (Sandbox Code Playgroud)

c++ str-replace c++14

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

标签 统计

str-replace ×10

php ×5

string ×4

python ×2

replace ×2

arrays ×1

c++ ×1

c++14 ×1

dataframe ×1

for-loop ×1

foreach ×1

html ×1

javascript ×1

pandas ×1

r ×1

regex ×1

substr ×1

url ×1