Skip to content
Advertisement

How to perform multiple search using php regex?

preg_match("/Designer:(.*?)|Created by:(.*?)n/i",$sitesource,$designer);

Hi, Can anyone tell me how can I perform multiple searches? For example, if the “designer” keyword not found, then search for “created by” and so on

Advertisement

Answer

You can use

preg_match_all("/(?:Created by|Designer):(.*)/i", $sitesource, $designer);

That is, use preg_match_all to match multiple occurrences and merge the two alternatives into a simpler (?:Created by|Designer):(.*) that means

  • (?:Created by|Designer) – either Created by or Designer
  • : – a colon
  • (.*) – capturing group #1: the rest of the line.

See the regex demo.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement