我有一个字符串,我想从中匹配并分隔以双连字符开头的每一行,并用h4元素替换这些字符串,而不使用与双连字符匹配的字符。
这是我的代码。
$rcp_ingredients = '--For Salad
1 1/2 Cup warm water (105°F-115°F)
2 1/4 Teaspoon active dry yeast
--For glaze
1 cup packed dark brown sugar
2 tablespoons finely chopped garlic
1 tablespoon Tabasco
--For Marination
3 tablespoons fresh lime juice
1 tablespoon fresh orange juice
1 tablespoon Dijon mustard
1 teaspoon curry powder, toasted
1/2 teaspoon salt
1/4 teaspoon black pepper
1/2 cup olive oil ';
$list = explode("\n", $rcp_ingredients);
foreach ( $list as $key => $data ) {
$data = preg_replace( '/\-{2}(\w+)/' , '<h4>$1</h4>', $data );
$pos = strpos( $data, '<h4>' );
if ( $pos === false ) {
echo '<span class="name">' . $data . '</span>';
} else {
echo $data;
}
}
输出
<h4>For</h4> Salad
<span class="name">1 1/2 Cup warm water (105°F-115°F)
</span><span class="name">2 1/4 Teaspoon active dry yeast
</span><h4>For</h4> glaze
<span class="name">1 cup packed dark brown sugar
</span><span class="name">2 tablespoons finely chopped garlic
</span><span class="name">1 tablespoon Tabasco
</span><h4>For</h4> Marination
<span class="name">3 tablespoons fresh lime juice
</span><span class="name">1 tablespoon fresh orange juice
</span><span class="name">1 tablespoon Dijon mustard
</span><span class="name">1 teaspoon curry powder, toasted
</span><span class="name">1/2 teaspoon salt
</span><span class="name">1/4 teaspoon black pepper
</span><span class="name">1/2 cup olive oil </span>
我想得到的输出是。
<h4>For Salad</h4>
<span class="name">1 1/2 Cup warm water (105°F-115°F)</span>
<span class="name">2 1/4 Teaspoon active dry yeast</span>
<h4>For Glaze </h4>
<span class="name">1 cup packed dark brown sugar</span>
<span class="name">2 tablespoons finely chopped garlic</span>
<span class="name">1 tablespoon Tabasco</span>
<h4>For Marination</h4>
<span class="name">3 tablespoons fresh lime juice</span>
<span class="name">1 tablespoon fresh orange juice</span>
<span class="name">1 tablespoon Dijon mustard</span>
<span class="name">1 teaspoon curry powder, toasted</span>
<span class="name">1/2 teaspoon salt</span>
<span class="name">1/4 teaspoon black pepper</span>
<span class="name">1/2 cup olive oil </span>
答案 0 :(得分:1)
试试这个希望这会帮助你..问题是
1。
(\w+)
对于像For Salad
这样的单词,它包含单词和空格,如果你想抓住所有内容,请使用--
开头,你可以使用^\s*\-{2}(.*)
<?php
ini_set('display_errors', 1);
$rcp_ingredients = '--For Salad
1 1/2 Cup warm water (105°F-115°F)
2 1/4 Teaspoon active dry yeast
--For glaze
1 cup packed dark brown sugar
2 tablespoons finely chopped garlic
1 tablespoon Tabasco
--For Marination
3 tablespoons fresh lime juice
1 tablespoon fresh orange juice
1 tablespoon Dijon mustard
1 teaspoon curry powder, toasted
1/2 teaspoon salt
1/4 teaspoon black pepper
1/2 cup olive oil ';
$list = explode("\n", $rcp_ingredients);
foreach ($list as $key => $data)
{
$data = preg_replace('/^\s*\-{2}(.*)/', '<h4>$1</h4>', $data);
$pos = strpos($data, '<h4>');
if ($pos === false)
{
echo '<span class="name">' . $data . '</span>';
} else
{
echo $data;
}
}
答案 1 :(得分:1)
使用(\w+)
,您只匹配第一个字(--
之后)。将其更改为&#34;任何内容&#34;在破折号后捕获整个字符串:
$data = preg_replace( '/\-{2}(.*)/' , '<h4>$1</h4>', $data );