php从2个单词之间的字符串中删除文本

时间:2015-04-26 22:38:10

标签: php preg-replace str-replace

我正在使用几个名为$ text的字符串。 在我的$ text字符串中,我有一个这样的段落:

{slimbox单个图像/故事TEXT TEXT TEXT ... ge.jpg,}

所有这些段落都以“{slimbox”开头,结尾宽度为“}”

我要做的是删除所有以“{slimbox”开头,结束宽度“}”开头的文字,包括“{slimbox”和width“}”。

我知道如何使用字符串替换和preg_replace,例如:

$text = preg_replace("/<img[^>]+\>/i", "", $text);

但是我不知道如何通过defing第一个字符和持续时间来删除整个字符串......

基本上,我把它作为输入:

  

{slimbox单身   图像/故事/采访/ KID_CREOLE / boys.jpg,图像/故事/采访/ KID_CREOLE / 00.jpg ,;   图像/故事/采访/ KID_CREOLE / creolecocosnafu2_80.jpg,图像/故事/采访/ KID_CREOLE / creolecocosnafu2_80.jpg ,;   图像/故事/采访/ KID_CREOLE / creolevan5_28_80.jpg,图像/故事/采访/ KID_CREOLE / creolevan5_28_80.jpg ,;   图片/故事/采访/ KID_CREOLE / KID-creole.jpg,图片/故事/采访/ KID_CREOLE / KID-creole.jpg ,;   图像/故事/采访/ KID_CREOLE / darnelltrax.jpg,图像/故事/采访/ KID_CREOLE / darnelltrax.jpg ,;   图像/故事/采访/ KID_CREOLE / kidguitar83stage.jpg,图像/故事/采访/ KID_CREOLE / kidguitar83stage.jpg,}桑达   (saepe enim redeo ad Scipionem,cuius omnis sermo erat de amicitia)   querebatur,quod omnibus in rebus homines diligentiores essent; capras   et quves quot quisque haberet,dicere posse,amicos quot haberet,non   在阿米奇斯,在伊利西斯,在伊利西斯,伊利西斯,阿迪克斯,阿迪克斯   eligendis neglegentis esse nec habere quasi signa quaedam et notas,   quibus eos qui ad amicitias essent idonei,iudicarent。 Sunt igitur   Firmi et stabiles et constantes eligendi; cuius generis est magna   penuria。 Et iudicare difficile est sane nisi expertum; experiendum   在ipsa amicitia的autem est。 Ita praecurrit amicitia iudicium tollitque   体验potestatem。

我需要这个作为输出:

  

Sed(saepe enim redeo ad Scipionem,cuius omnis sermo erat de   amicitia)querebatur,quod omnibus in rebus homines diligentiores   埃森特; capras et oves quot quisque haberet,dicere posse,amicos quot   haberet,non posse dicere et in illis quidem parandis adhibere curam,   在amicis eligendis neglegentis esse nec habere quasi signa quaedam et   notas,quibus eos qui ad amicitias essent idonei,iudicarent。 SUNT   igitur firmi et stabiles et constantes eligendi; cuius generis est   magna penuria。 Et iudicare difficile est sane nisi expertum;   在ipsa amicitia的经验autem est。 Ita praecurrit amicitia   iudicium tollitque experiendi potestatem。

任何人都可以帮我这个吗?

非常感谢,

1 个答案:

答案 0 :(得分:0)

[7.499999, 7.599999]

输出:

$string = <<< LOB
{slimbox single images/stories/INTERVIEW/KID_CREOLE/boys.jpg,images/stories/INTERVIEW/KID_CREOLE/00.jpg,; images/stories/INTERVIEW/KID_CREOLE/creolecocosnafu2_80.jpg,images/stories/INTERVIEW/KID_CREOLE/creolecocosnafu2_80.jpg,; images/stories/INTERVIEW/KID_CREOLE/creolevan5_28_80.jpg,images/stories/INTERVIEW/KID_CREOLE/creolevan5_28_80.jpg,; images/stories/INTERVIEW/KID_CREOLE/kid-creole.jpg,images/stories/INTERVIEW/KID_CREOLE/kid-creole.jpg,; images/stories/INTERVIEW/KID_CREOLE/darnelltrax.jpg,images/stories/INTERVIEW/KID_CREOLE/darnelltrax.jpg,; images/stories/INTERVIEW/KID_CREOLE/kidguitar83stage.jpg,images/stories/INTERVIEW/KID_CREOLE/kidguitar83stage.jpg,}Sed (saepe enim redeo ad Scipionem, cuius omnis sermo erat de amicitia) querebatur, quod omnibus in rebus homines diligentiores essent; capras et oves quot quisque haberet, dicere posse, amicos quot haberet, non posse dicere et in illis quidem parandis adhibere curam, in amicis eligendis neglegentis esse nec habere quasi signa quaedam et notas, quibus eos qui ad amicitias essent idonei, iudicarent. Sunt igitur firmi et stabiles et constantes eligendi; cuius generis est magna penuria. Et iudicare difficile est sane nisi expertum; experiendum autem est in ipsa amicitia. Ita praecurrit amicitia iudicium tollitque experiendi potestatem.
LOB;

preg_match_all('/}(.*?)$/sim', $string , $matches, PREG_PATTERN_ORDER);
echo $matches[1][0];

演示

http://ideone.com/fPgv52