Regular expression for every line starting with double hyphens

I have a string from which I want to match and split each line starting with double hyphens and replace those lines with h4 elements without characters matching double hyphens.

Here is the code I have.

$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;
    }
}

      

results

<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>

      

I want to get the result.

<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>

      

+3


source to share


2 answers


Try this hope, it will help you. The problem was

1.for a (\w+)

word like For Salad

it contains words as well as spaces, if you want everything to start with --

you can use^\s*\-{2}(.*)



Try this piece of code here

<?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


source


With (\w+)

you only match the first word (by following --

). Change it to "all" to capture the entire line after the dash:



$data = preg_replace( '/\-{2}(.*)/' , '<h4>$1</h4>', $data );

      

+1


source







All Articles