1

私がやろうとしているのは、存在する場合は、「ショートコード」内のテキストの出現を削除することです。たとえばHere's some content [shortcode]I want this text removed[/shortcode] Some more content、に変更しHere's some content [shortcode][/shortcode] Some more contentます。

とても簡単なことのように思えますが、理解できません.. =/

ショートコードは、文字列全体で 1 回だけ表示されます。

助けてくれてありがとう。

4

7 に答える 7

2

これを試して:

$var = "Here's some content [shortcode]I want this text removed[/shortcode] Some more content";
$startTag = "[shortcode]";
$endTag = "[/shortcode]";
$pos1 = strpos($var, $startTag) + strlen($startTag);
$pos2 = strpos($var, $endTag);
$result = substr_replace($var, '', $pos1, $pos2-$pos1);
于 2011-10-31T10:34:12.970 に答える
2

preg_replace() で行うのは非常に簡単です。あなたの目的のために、/\[shortcode\].*\[\/shortcode\]/パターンとして使用してください。

$replace = "[shortcode][/shortcode]"; 
$filteredText = preg_replace("/\[shortcode\].*\[\/shortcode\]/", $replace, $yourContent);

詳細については、 http://php.net/manual/en/function.preg-replace.phpを参照してください。

于 2011-10-31T10:43:37.510 に答える
1

通常のエクスプレッションに煩わされたくない場合:

文字列内にタグがある場合[shortcode]は、実際には問題ありません。substr をネストして使用するだけです。

substr($string,0,strpos($string,'[substring]')+11)+substr($string,strpos($string,'[/substring]'),strlen($string))

ここで、最初の substr は文字列をカットする文字列の先頭までカットし、2 番目は文字列の残りの部分を追加します。

ここを参照してください:

http://www.php.net/manual/en/function.substr.php

http://www.php.net/manual/en/function.strpos.php

于 2011-10-31T10:34:39.993 に答える
1

strpos()を使用して、文字列内の[サブストリング]と[/substring]の位置を見つけ、substr_replace()を介してテキストをWhitespaceに置き換えることができます。

于 2011-10-31T10:27:51.270 に答える
0
$string = "[shortcode]I want this text removed[/shortcode]"; 
$regex = "#\[shortcode\].*\[\/shortcode\]#i"; 
$replace = "[shortcode][/shortcode]"; 
$newString = preg_replace ($regex, $replace, $string, -1 );  
于 2011-10-31T10:32:36.047 に答える
0
$content = "Here's some content [shortcode]I want this text removed[/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content";
print preg_replace('@(\[shortcode\])(.*?)(\[/shortcode\])@', "$1$3", $content);

収量:

ここにいくつかのコンテンツがあります [ショートコード][/ショートコード] 変更されるいくつかのコンテンツ ここにいくつかのコンテンツがあります [ショートコード][/ショートコード] いくつかのコンテンツ

于 2011-10-31T10:32:54.040 に答える
0

それを取り除くにはphpで正規表現を使用してください。

preg_replace (shortcode, urText, '', 1)
于 2011-10-31T10:27:09.570 に答える