标签: preg-replace

RegEx,preg_replace_callback问题PHP

这就是我为RegEx所得到的,我想知道这是否是最佳方式.

我希望能够找到类似的东西,无论标识符之间的间距如何,都不区分大小写.如果可能的话,不用担心订单..

例:

[Foreclosure ="Remax" URL="http://www.remax.com" Title = "4 Bedroom 2 Bath Condo"]
[Foreclosure ="Remax"URL="http://www.remax.com"Title="4 Bedroom 2 Bath Condo"]
[Foreclosure  =   "Remax"    URL="http://www.remax.com"     Title = "4 Bedroom 2 Bath Condo"    ]
Run Code Online (Sandbox Code Playgroud)

这是我现有的代码:

function ForeclosureCode_filter( $buffer )
{
    //There might be a better way to do the regex...  But this seems to work...
    $buffer = preg_replace_callback( '@\[Forclosure *=*"(.*?)" *url *=*"(.*?)" *title="(.*?)" *\]@si',
        "ForeclosureCode_replace", $buffer );
    return $buffer;
}
Run Code Online (Sandbox Code Playgroud)

php regex preg-replace preg-replace-callback

0
推荐指数
1
解决办法
168
查看次数

如果字符串以"00"{number}或"+"{number}开头,则为Preg_match

我必须测试字符串是以00还是+开头.

伪代码:

Say I have the string **0090** or **+41** 
if the string begins with **0090** return true,  
elseif string begins  with **+90** replace the **+** with **00**  
else return false
Run Code Online (Sandbox Code Playgroud)

最后两位数字可以是0-9.
我怎么在PHP中这样做?

php regex preg-replace

0
推荐指数
1
解决办法
784
查看次数

正则表达式用'选项'替换'li'而不会丢失class和id属性

我正在寻找使用preg_replace或类似方法来改变的解决方案:

<li id="id1" class="authorlist" />
<li id="id2" class="authorlist" />
<li id="id3" class="authorlist" />
Run Code Online (Sandbox Code Playgroud)

<option id="id1" class="authorlist" />
<option id="id2" class="authorlist" />
<option id="id3" class="authorlist" />
Run Code Online (Sandbox Code Playgroud)

我想我的模式是正确的,但不知道如何更换零件......

这是(wordpress)php代码:

$string = wp_list_authors( $args ); //returns list as noted above
$pattern = '<li ([^>]*)';
$replacement = '?????';
echo preg_replace($pattern, $replacement, $string);
Run Code Online (Sandbox Code Playgroud)

建议好吗?

php regex preg-replace

0
推荐指数
1
解决办法
658
查看次数

<a>标签替换的正则表达式

我是正则表达式的新手,但我正在努力学习它.我想删除html文本的标记,只允许内部文本.像这样的东西:

Original: Lorem ipsum <a href="http://www.google.es">Google</a> Lorem ipsum <a href="http://www.bing.com">Bing</a>
Result:  Lorem ipsum Google Lorem ipsum Bing
Run Code Online (Sandbox Code Playgroud)

我正在使用此代码:

$patterns = array( "/(<a href=\"[a-z0-9.:_\-\/]{1,}\">)/i", "/<\/a>/i");
$replacements = array("", "");

$text = 'Lorem ipsum <a href="http://www.google.es">Google</a> Lorem ipsum <a href="http://www.bing.com">Bing</a>';
$text = preg_replace($patterns,$replacements,$text);
Run Code Online (Sandbox Code Playgroud)

它有效,但我不知道这个代码是更高效还是更易读.

我可以用某种方式改进代码吗?

html php regex preg-replace

0
推荐指数
2
解决办法
1002
查看次数

php中的正则表达式从wiki文本中删除引文

从给定的示例文本中,我希望文本与[[]]和{{}}中包含的文本区分开来

示范文本:

1988年12月11日,年仅15岁零232天,Tendulkar在[[孟买板球队|孟买]]对阵[[古吉拉特板球队]队的首场[[一流板球|一流]]比赛中得分100分|古吉拉特邦],让他成为最年轻的印第安人,在一流的首秀中获得一个世纪的成绩.他在他的第一个Deodhar和Duleep Trophy中打入了一个世纪.{{cite web | url = http://www.espnstar.com/cricket/international-cricket/news/detail/item136972/Sachin-Tendulkar-factfile/ | title = Sachin Tendulkar factfile | publisher = www.espnstar.com |访问日= 2009年8月3日}}他被孟买队长[[Dilip Vengsarkar]]选中后,看到他在篮网中谈判[[卡皮尔开发]],并在本赛季结束时成为孟买最高得分手.他得到583分平均为67.77,并且是整体得分第六高的{{cite web | url = http://blogs.cricinfo.com/link_to_database/ARCHIVE/1980S/1988-89/IND_LOCAL/RANJI/STATS/IND_LOCAL_RJI_AVS_BAT_MOST_RUNS.html | title = 1988-89 Ranji season - Most Runs | publisher = Cricinfo | accessdate = 2009年8月3日}}他在[[Irani Trophy]]决赛中也创造了一个不败的世纪,{{cite web | url = http:// cricketarchive.com/Archive/Scorecards/52/52008.html|title=Rest of India v Delhi in 1989/90 | publisher = Cricketarchive | accessdate = 2009年8月3日}}并在明年被选中参加巴基斯坦之旅一流的海洋 上.

我试过这个:

$patterns = ("/^{{*/", "/*}}$/" );$replacements = "";
  preg_replace($patterns, $replacements, $parts);
  print_r($parts);
Run Code Online (Sandbox Code Playgroud)

还有这个:

$parts …
Run Code Online (Sandbox Code Playgroud)

php regex preg-replace html-parsing

0
推荐指数
1
解决办法
859
查看次数

正则表达式:删除空格,但不删除<>标记

我一直在努力使用regexp很长时间..我需要的是从字符串中删除所有空格,但不是从<tags>中删除(例如,一个href标签或img src标签必须有空格).

我在PHP中这样做,我正在尝试不同的解决方案(我是regexp的总菜鸟,这对我来说太混乱了).到目前为止,这是我的代码的一部分:

$text=$_POST["text"];
$pattern = '(\<.+?\>)|\s'; 
$replace = '';
echo preg_replace( $pattern, $replace, $text );
Run Code Online (Sandbox Code Playgroud)

这有什么问题?:(

php regex string preg-replace

0
推荐指数
1
解决办法
3335
查看次数

preg_replace如何改变uri的一部分

我试图用php preg_replace更改html的所有链接.所有的uris都有以下形式

http://example.com/page/58977?forum=60534#comment-60534
Run Code Online (Sandbox Code Playgroud)

我想将其更改为:

http://example.com/60534
Run Code Online (Sandbox Code Playgroud)

这意味着删除"page"之后和"comment-"之前的所有内容,包括这两个字符串.

我尝试了以下内容,但它没有返回任何更改:

$result = preg_replace("/^.page.*.comment-.$/", "", $html);
Run Code Online (Sandbox Code Playgroud)

但似乎我的正则表达式语法不正确,因为它返回html不变.你能帮帮我吗?

php regex preg-replace

0
推荐指数
1
解决办法
232
查看次数

删除每个单词的最后一个字母,如果它是字母"s"

如标题中所示,我有一个句子带有一些单词,如果它是s句子的每个单词的字母,我想删除最后一个字母.

我试试这个

preg_replace("%s(?!.*s.*)%", "", $mystring);
Run Code Online (Sandbox Code Playgroud)

但它只删除了最后一个字

php regex replace preg-replace

0
推荐指数
1
解决办法
1206
查看次数

如何删除字符串中的所有非大写字符?

是的,我基本上只是试图引爆一个短语像Social Inc.David JasonSIDJ.我试过使用爆炸但是无法弄清楚如何爆炸BUT大写字母的一切,我需要使用preg_match()吗?

php regex preg-replace uppercase

0
推荐指数
2
解决办法
1377
查看次数

如何获得URL的一部分?

如何从网址中删除除基本网址和第一部分以外的所有部分。零件数量不确定。基本网址是可变的。我尝试了一些正则表达式,但没有成功。

$url =  http://www.example.com/part1/part2/part3/part4;
base_url = parse_url($url, PHP_URL_HOST); // Outputs www.example.com

$desired_output = http://www.example.com/part1;
Run Code Online (Sandbox Code Playgroud)

php regex preg-replace regex-group regex-greedy

0
推荐指数
1
解决办法
57
查看次数