[英]Regular Expression for each line starting with double hyphens
我有一个字符串,我想从中匹配并分隔以双连字符开头的每一行,并用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>
试试这个希望这会帮助你..问题是
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;
}
}
使用(\\w+)
您只匹配第一个单词(后面的--
)。 将其更改为“任何”以捕获破折号后面的整个字符串:
$data = preg_replace( '/\-{2}(.*)/' , '<h4>$1</h4>', $data );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.