Looking for Regular Expressions in PHP

I'm using preg_match function in PHP in order to extract some values from a RSS Feed. Inside this feed content there is something like this:

<li><strong>Something:</strong> A text with non alphanumeric characters (more text), more text with non alphanumeric characters (more text)</li>

I need to get those "A text with non alphanumeric characters" and "more text with non alphanumeric characters" to save them in a database. I don't know if using regular expressions is the best way to do it.

Thank you so much.

Answers


If you want to use regex (i.e. quick and dirty, not really too maintainable), this will give you the text:

$input = '<li><strong>Something:</strong> A text with non alphanumeric characters (more text), more text with non alphanumeric characters (more text)</li>';

// Match between tags
preg_match("#</strong>(.*?)</li>#", $input, $matches);
// Remove the text inside brackets
echo trim(preg_replace("#\s*\(.*?\)\s*#", '', $matches[1]));

Though, nested brackets may fail.


Given that the structure is always the same you can use this regex

</strong>([^,]*),([^<]*)</li>

group 1 will have the first fragment, group 2 the other

Once you start parsing html/xml with regexes it becomes quickly apparent that a full blown parser is better suited. For small or throwaway solution you a regex can be useful.


$str = '<li><strong>Something:</strong> A text with non alphanumeric characters (more text), more text with non alphanumeric characters (more text)</li>';
$str = preg_replace('~^.*?</strong>~', '', $str); // Remove leading markup
$str = preg_replace('~</li>$~', '', $str); // Remove trailing markup
$str = preg_replace('~\([^)]++\)~', '', $str); // Remove text within parentheses
$str = trim($str); // Clean up whitespace
$arr = preg_split('~\s*,\s*~', $str); // Split on the comma

Need Your Help

Error: org.json.JSONException: End of input at character 0 of

android json

I have been playing around with some JSON, trying to get and display the strings to show in a TextView.

Upload Multiple images using Volley Library in android

android android-volley

I have tried to upload multiple images using volley library in a single web service. but, only the last image is getting uploaded. Those previous images are getting replaced with the null. I want t...