对我来说,使用这个正则表达式非常有趣。我有一个包含几行的字符串列表。让我们说
$ingredients = '--For Salad ~20 mins~
1 1/2 Cup warm water (105°F-115°F)
2 1/4 Teaspoon active dry yeast
1 Cup packed dark brown sugar
2 tbsp Finely chopped garlic
1 tbsp Tabasco
--For Marination ~35 mins~
3 tbsp Fresh lime juice
1 tbsp Fresh orange juice
1 tbsp Dijon mustard
1 tsp Curry powder, toasted
1/2 teaspoon salt
1/4 teaspoon black pepper
1/2 cup olive oil ';
$list = explode("\n", $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;
}
}
如何匹配另一个模式并将其替换为其他元素并附加到另一个字符串替换内。
在第一行中,以双连字符开头的字符串包含以Tilde ~
开头和结尾的参数。
我想要的是为每一行获取其他模式并输出它。
我想看下面的输出。
<h4>For Salad <span class="duration">20 mins</span></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>
<span class="name">1 Cup packed dark brown sugar</span>
<span class="name">2 tbsp Ffinely chopped garlic</span>
<span class="name">1 tbsp Tabasco</span>
<h4>For Marination <span class="duration">35 mins</span></h4>
<span class="name">3 tbsp Fresh lime juice</span>
<span class="name">1 tbsp Fresh orange juice</span>
<span class="name">1 tbsp Dijon mustard</span>
<span class="name">1 tsp 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与其他模式分开获取。
<h4>For Salad</h4><span class="duration">20 mins</span>
答案 0 :(得分:2)
答案 1 :(得分:1)
在替换任何内容之前,请尝试决定如何处理每行:
foreach ($list as $key => $data)
{
$matches = [];
if (preg_match('/^--(.*)~(.*)~$/', $data, $matches) {
echo '<h4>' . $matches[1] . '</h4>' .
'<span class="duration">' . $matches[2] . '</span>';
} else {
echo '<span class="name">' . $data . '</span>';
}
}
答案 2 :(得分:1)
希望这会对您有所帮助,我们正在搜索~
并将~([^~]+)~
替换为span
标签。
1。
~([^~]+)~
这将匹配~
之间的所有字符,包括~
的
<?php
ini_set('display_errors', 1);
$ingredients = '--For Salad ~20 mins~
1 1/2 Cup warm water (105°F-115°F)
2 1/4 Teaspoon active dry yeast
1 Cup packed dark brown sugar
2 tbsp Ffinely chopped garlic
1 tbsp Tabasco
--For Marination ~35 mins~
3 tbsp Fresh lime juice
1 tbsp Fresh orange juice
1 tbsp Dijon mustard
1 tsp Curry powder, toasted
1/2 teaspoon salt
1/4 teaspoon black pepper
1/2 cup olive oil ';
$list = explode("\n", $ingredients);
foreach ($list as $key => $data)
{
$data = preg_replace('/^\s*\-{2}(.*)/', '<h4>$1</h4>', $data);
$pos = strpos($data, '~');
if ($pos !== false)
{
echo $data = preg_replace("/~([^~]+)~/", '<span class="duration">$1</span>', $data);
}
else
{
//replacing data with span tags around it
echo $data = str_replace($data, '<span class="name">'.$data.'</span>', $data);
}
echo PHP_EOL;
}
解决方案2:(another format
)Try this code snippet here
foreach ($list as $key => $data)
{
if (preg_match('/^\s*\-{2}([^~]+)(~[^~]+~)/', $data,$matches) && strpos($data, '~') !== false)
{
echo "<h4>".$matches[1]."</h4>";
echo '<span class="duration">'.$matches[2].'</span>';
}
else
{
//replacing data with span tags around it
echo $data = str_replace($data, '<span class="name">'.$data.'</span>', $data);
}
echo PHP_EOL;
}