How to parse a string without losing plus sign in PHP?
<p>I am parsing HTML strings to get values in PHP and write them in database. Here is an example string:</p>

<pre><code>&lt;b&gt;Adress:&lt;/b&gt; 22 Examplary road, Nowhere &lt;br&gt;
&lt;b&gt;Phone:&lt;/b&gt;  +371 12345678, +371 23456789&lt;br&gt;
&lt;b&gt;E-mail: &lt;/b&gt;info@example.com&lt;br&gt;
</code></pre>

<p>The string can be formatted in random manners. It can contain additional keys that I am not parsing out and it can contain duplicate keys. It can also contain only some of the keys that I am interested in or be completely empty. HTML can also be broken (example tag: <code>&lt;br</code>). I have decided that I will follow the rules that entries are separated by <code>\n</code> and are in the form <code>key: value</code> + some HTML.</p>

<p>First, I use this code to make the string parseable:</p>

<pre><code>$parse = strip_tags($string);
$parse = str_replace(':', '=', $parse);
$parse = str_replace("\n", '&amp;', $parse);
$parse = str_replace("\r", '', $parse);
$parse = str_replace("\t", '', $parse);
</code></pre>

<p>My string looks something like this now:</p>

<pre><code>Adress= 22 Examplary road, Nowhere&amp;Phone=  +123 12345678, +123 23456789&amp;E-mail= info@example.com
</code></pre>

<p>Then I use <code>parse_str()</code> to get the values and then I take out the values if the needed keys are found:</p>

<pre><code>        parse_str($parse, $values);

        $address = null;
        if (isset($values['Adress']))
            $address = trim($values['Adress']);

        $phone = null;
        if (isset($values['Phone']))
            $phone = trim($values['Phone']);
</code></pre>

<p>The problem is that I end up with <code>$phone = '371 12345678, 371 23456789'</code> - I lose the <code>+</code> signs. How to conserve those?</p>

<p>Also, if you have any hints how to improve this procedure, I would be glad to know that. Some entries have <code>Website: example.com</code>, others have <code>Web Site example.com</code>... I am pretty sure that it will not be possible to automatically parse all of the information but I am looking for the best possible solution.</p>

<h2>Solution</h2>

<p>Using tips provided by WEBjuju I am now using this:</p>

<pre><code>preg_match_all('/([^:]*):\s?(.*)\n/Usi', $string, $matches, PREG_SET_ORDER);

$values = [];
foreach ($matches as $match)
{
    $key = strip_tags($match[1]);
    $key = trim($key);
    $key = mb_strtolower($key);
    $key = str_replace("\s", '', $key);
    $key = str_replace('-', '', $key);

    $value = strip_tags($match[2]);
    $value = trim($value);

    $descriptionValues[$key] = $value;
}
</code></pre>

<p>This allows me to go from this input:</p>

<pre><code>&lt;b&gt;Venue:&lt;/b&gt; The Hall&lt;br
&lt;b&gt;Adress:&lt;/b&gt; 22 Examplary road, Nowhere &lt;br&gt;
&lt;b&gt;Phone:&lt;/b&gt;  +371 12345678&lt;br&gt;
&lt;b&gt;E-mail: &lt;/b&gt;info@hkliepaja.lv&lt;br&gt;
&lt;b&gt;Website:&lt;/b&gt; &lt;a href="http://example.com/" target="_blank"&gt;example.com&lt;/a&gt;&lt;br&gt;
</code></pre>

<p>To a nice PHP array with homogenized and hopefully recognizable keys:</p>

<pre><code>[
    'venue' =&gt; 'The Hall',
    'adress' =&gt; '22 Examplary road, Nowhere',
    'phone' =&gt; '+371 12345678',
    'email' =&gt; 'info@example.com',
    'website' =&gt; 'example.com',
];
</code></pre>

<p>It still doesn't account for the cases of missing colons, but I don't think I can solve that...</p>