使用PHP替换字符串中的文本
我有这个字符串: $my_string = "http://apinmo.com/1/2/3.jpg 4444/8888/7777 http://apinmo.com/4/5/8-1.jpg"; 我需要删除URL的最后两个斜线以获取此功能,其余的则保持不变: $my_NEW_string = "http://apinmo.com/123.jpg 4444/8888/7777 http://apinmo.com/458-1.jpg"; 我尝试了: $my_NEW_string = preg_replace('/(?
0 2023-06-02
编程技术问答社区
如何在一个目录中的每个文件内用'php'替换'php3'?
希望一个不错的简单. 我有一个我想在PHP 5.2 上运行的PHP3网站 首先,我只想参考当前的" index.php3" _within_each_file_(递归)更改为" index.php",然后继续担心全球等等. . k.去! :) 更新:非常感谢!我意识到我的问题错过了网站每个文件中的参考文献,我想在所有文件/文件夹中递归地进行. 解决方案 find -type f -exec perl -pi -e 's/\bindex\.php3\b/index.php/g' {} \; 其他解决方案 怎么样: for file in *.php3 do sed 's/\.php3/.php/g' $file > ${file%3} done for/do/done循环依次处理每个.php3文件,编辑文件并用.php替换每个字符串.php3,然后将文件从something.php3复制到something.php. > 为修
0 2023-06-02
编程技术问答社区
PHP替换HTML格式化文本中某些字符串模式的所有事件
我的输入文本可以是带有标签的html文本,依此类推.输入的示例可以是: Lorem ipsum dolor sit amet, consectetur adipiscing elit. %image1% Lorem ipsum Cum sociis natoque penatibus et magnis dis parturient montes. %image2% Lorem ipsum dolor sit amet, consectetur adipiscing elit. %image3% Lorem ipsum dolor sit amet, consectetur adipiscing elit. ... 找到%%字符之间所有文本出现并用替换的最简单方法是什么? 解决方
0 2023-06-02
编程技术问答社区
如何在字符串中的"@"字符的前面提取所有文本
如何删除从'@'? 开始的字符串 例如admin@admin.com,我想删除从 @启动的字符串,因此它仅变成" admin". 就像在Twitter中一样..我阅读了有关STR替换和修剪的信息,但我认为还有其他方法可以做到吗? $email = 'admin@admin.com'; echo substr_replace($email, ?, ?) ; this i cant do 解决方案 您不需要替换剩余的,您只需切出 .在这种情况下 $name = strtok($email, "@"); 其他解决方案 尝试此功能: strstr : $start = strstr($email, '@', true); 其他解决方案 怎么办: substr($email, 0, strpos($email, '@'));
0 2023-06-02
编程技术问答社区
使用JS替换整个页面时出现错误
我有一个带有内容的php变量是html: 当我使用JavaScript时: document.body.innerHTML.replace(""); 要替换整页,它说错误: 和jQuery是相同的错误: jQuery('html').html(""); 我可以做任何这样做的方法,JS和PHP解决方案还可以. 解决方案 似乎不太了解引号和特殊字符,而更多地涉及新线. 您可以使用JSON防止所有这些角色出现. document.body.innerHTML = ; 这在我的服务器上起作用: Lorem Ipsum is simply dummy text of the printing and typesetting industry.
0 2023-06-02
编程技术问答社区
PHP用图像替换文本
这将是一系列数据: $array = array("sad","bow","blabla"); 如果 $input = "I love Stackoverflow! :bow: so much"; 输出应为: I love Stackoverflow so much 做到这一点的最佳和最快的方法是什么? 解决方案 $array = array(':('=>"sad",'-_-'=>"bow",'bla'=>"blabla"); $input = "I love Stackoverflow! :bow: so much"; $output = str_replace(array_keys($array), array_values($array), $input); 编辑:对不起........ 尝试2: preg_replace('`:(\w+):`', '
0 2023-06-02
编程技术问答社区
删除帖子报价的正则
说我有这个文本: [quote=XXXXXX]ABC[quote=YYYYYY]DEF[/quote]GHI[/quote]JKL[quote=ZZZZZ]MNO[/quote] 如何在[QUOTE]和[/QUOTE]标签之间删除所有文本,即,以上文本将变为JKL([QUOTE = xxxxx] ... [/QUOTE]获取删除和[quote = zzzzz] ... [/quote]).请注意,它不应替换所有文本,因为它以报价标签启动和结尾,也不应替换[quote=XXXXX]ABC[quote=YYYYY]DEF[/quote].这是否可以使用Regex? 感谢您的回答! :) 解决方案 要匹配嵌套结构,您可以编写递归模式(一种用(?R)引用自身的模式): $pattern = '~\[quote\b[^]]*][^[]*+(?:\[(?!/?quote\b)[^[]*|(?R)[^[]*)*+\[/quote]~i'; $txt = preg_repl
0 2023-06-02
编程技术问答社区
PHP替换全部
我在JavaScript中有一个: msg = msg.replace(/(:\)|=\)|:-\)|\(:)/gi, ""); 我可以做类似的方法,但是在PHP中? 提前感谢,enji 解决方案 完全相同: $msg = preg_replace('/(:\)|=\)|:-\)|\(:)/i', "", $msg); 其他解决方案 如果我理解正确,您正在尝试用图像替换:\ Smiley的实例. 您可以做这样的事情:
0 2023-06-02
编程技术问答社区
如何用星星替换手机号码,除了PHP中的最后4位数字
我试图用星星替换手机号码,除了文本中的最后4位数字,并且文本是动态的. Eg. John's Mobile number is 8767484343 and he is from usa. Eg. John's Mobile number is +918767484343 and he is from india. Eg. Sunny's Mobile number is 08767484343 and he is from india. Eg. Rahul's Mobile number is 1800-190-2312 and he is from india. $dynamic_var = "John's Mobile number is 8767484343 and he is from usa."; $number_extracted = preg_match_all('!\d+!', $dynamic_var , $contact_number); // do
0 2023-06-02
编程技术问答社区
使用PREG_Match用PHP变量替换标签
{$thisname}应转换为. {thisname}应转换为. {$this.movie.games} && {$this.new}应分别转换为和. 解决方案 $tpl = 'Name: {$name}, Surname: {surname}, City: {$person.address.city}'; function tpl2php($m){ $var = $m[1]; if(strpos($var,'.')){ $varArr = explode('.',$var); $var = $varArr[0]; for($i=1;$i
0 2023-06-02
编程技术问答社区
如何用数组中的元素替换所有出现的char?
我有以下字符串: $sql = sql_prepare("UPDATE `test` SET `item1` = ?, `item2` = ?, `item3` = ?", array(50, 55, 60)); 我需要创建" sql_prepare"函数,以匹配'?'的所有情况char并用数组中的元素替换,因此最终的SQL将是: UPDATE `test` SET `item1` = 50, `item2` = 55, `item3` = 60 我该怎么做? 解决方案 当MySQLI和PDO都实现真实准备的语句时,为什么尝试使用传统mysql_* api伪造准备的语句? PDO示例: if ($prepped = $pdo -> prepare ('UPDATE `test` SET `item1` = ?, `item2` = ?, `item3` = ?')) { $res = $prepped -> execute (array (
0 2023-06-02
编程技术问答社区
在指定的辅音之后添加" E",如果尚未遵循元音
我正在尝试使用str_replace()替换辅音,而辅音则是带有辅音的元音,然后是e.因此,lb应成为leb. 相反,像la之类的字符串不应变为lea,而应保持la,因为l后面是元音. $phrase = "ol sonf"; $key = ["l", "n", "s" ,"f"]; $replace = ["le","ne","se","fe"]; $newPhrase = str_replace($key, $replace, $phrase); echo $phrase; echo ""; echo $newPhrase; 结果: ol sonf = ole seonefe ,但应该是: ol sonf = ole sonefe s 是一种辅音,但是由于它毗邻元音 so - 在这种情况下,它不应更换,因此应将相同的规则应用于所有元音 a,e,i,o,u . 我的意思是它应该仅适用于e 无声辅音. 解决
0 2023-06-02
编程技术问答社区
如何替换文本,除非在PHP中的两个标签之间进行替换?
我有一个语句替换要在PHP中进行(使用str_replace或其他内容).我有这句话: the sun is yellow when the red sun is not yellow 我想要这个: the apple is yellow when the red sun is not yellow 所以我需要替换一个单词,除了两个特定标签()之间.如何使用str_replace或使用正则表达式? 解决方案 如果字符串很小,为什么不使用explode()在标签上爆炸, 并在所需片段中更换字符串,然后重新加入? 或尝试domdocument:
0 2023-06-02
编程技术问答社区
仅在PHP中替换<tag>和</tag>之间的特定字符
我有类似 的东西,我想得到它: <1> <2> ,但我只想在标签中应用于> . 我已经有一个: $txt = $this->input->post('field'); $patterns = array( "other stuff to find", "/.*(/m" ); $replacements = array( "other stuff to replace", "<" ); $records = preg_replace($patterns,$replacements, $txt); 它成功替换了字符,但它删除了周围的标签 任何帮助将不胜感激!谢谢 解决方案 其他可能性,使用回调功能:
0 2023-06-02
编程技术问答社区
PHP替换除HTML标签以外的字符
我需要在字符串中用\xD9\xA0,\xD9\xA1,\xD9\xA2,...,\xD9\xA9替换字符0,1,2,...,9.该字符串来自CKEditor,因此它可能包含HTML标签.使用以下代码 $body = str_replace("1", "\xD9\xA1", $body); 它用\xD9\xA1代替每个1,因此它会影响标签 以及 ,而我只需要替换体内的数字而不是标签. 包含数字的标签为 和cellspacing和cellpadding和cellpadding和border table标签. 我如何将数字替换为上述符号,而它不会影响 和cellspacing和cellpadding和border? 解决方案 您不应该使用正则义务来处理HTML,但是,如果您仍然想使用正则票据,则可以使用以下等级的丢弃模
0 2023-06-02
编程技术问答社区
使用PHP替换所有域名(包括子域名)
我在PHP中使用preg_replace或str_replace替换所有域名,包括www $string = ' https://example.com, https://www.example.com, https://subdomain.example.com '; $olddomain= "example.com"; $newdomain = "stackoverflow.com"; $output = str_replace($olddomain, $newdomain, $string); $output = preg_replace('#(www[.])?[.]' . $olddomain. '#', $newdomain, $body); echo $output; 我的期望: https://example.com -> https://stackoverflow.com https://www.example.com -> https://stac
0 2023-06-01
编程技术问答社区
PHP用一个HTML断句代替双行
我试图用 标签替换服务器上的所有\ n \ n,以便单个\ n不会变成 . 示例: Hello,\n\nThis is an\nexample.\n\nThanks! 去: Hello, This is an\nexample, Thanks! (注意单\ n未替换) 当我在PHP中进行以下操作时,它不会用休息来代替两行: $str = str_replace("\n\n", " ", $str); 解决方案 您的\n实际上是\r\n(这意味着输入来自Windows操作系统),我建议您首先将新线标准化到 *NIX标准,并使用以下正则表达式: $str = preg_replace('~\r\n?~', "\n", $str); 然后,您的原始片段将有效( demo ): $str = str_replace("\n\n", ' ', $str); 您也可以做: $str =
0 2023-06-01
编程技术问答社区
在php中,我如何使用preg替换将URL变成tinyurl
我需要将包含长URL的文本字符串转换为同一字符串,但使用tinyurl(使用Tinyurl API).例如.转换 blah blah blah http://example.com/news/sport blah blah blah 进入 blah blah blah http://tinyurl.com/yaeocnv blah blah blah 如何完成? 解决方案 为了缩短文本中的任意数量的URL,将API内容放入函数中,该功能占用长URL并返回短URL.然后,通过PHP的preg_replace_callback函数应用此功能.看起来像这样:
0 2023-06-01
编程技术问答社区
用不同的值替换字符串的多个出现
我有一个脚本,该脚本生成包含某些令牌的内容,我需要替换每个出现令牌,而不同的内容由单独的循环产生. 使用str_replace用相同 content替换令牌的所有出现,但是我需要用循环的下一个结果替换每个出现. 我确实看到了这个答案:搜索并用php5中的多个/不同值替换多个值? 但是,它正在从预定的阵列中工作,我没有. 样本内容: This is an example of %%token%% that might contain multiple instances of a particular %%token%%, that need to each be replaced with a different piece of %%token%% generated elsewhere. 我需要用这个简单的循环来代替每次出现%%令牌%%的内容: for($i=0;$i
0 2023-06-01
编程技术问答社区
正则是在数字之间加入数字时的空间
我正在尝试构建一个正则正等式,在它们之间有空间时,它们在字符串之间加入数字,例如: $string = "I want to go home 8890 7463 and then go to 58639 6312 the cinema" 正则应输出: "I want to go home 88907463 and then go to 586396312 the cinema" 正则可以使用Python或PHP语言. 谢谢! 解决方案 使用look-pavenave,查看下一个块是否是一组数字并删除尾随空间.这样,它适用于任意数量的集合(我怀疑您可能想要): $string = "I want to go home 8890 7463 41234 and then go to 58639 6312 the cinema"; $newstring = preg_replace("/\b(\d+)\s+(?=\d+\b)/", "$1", $stri
0 2023-06-01
编程技术问答社区