Here’s my code, but it’s not working. How can I match the word before dash?
$str = '-Lorem Ipsum is simply dummy- text of the printing';
preg_match('/(.*)\-/', $str, $matches);
Expected Output: dummy
Thanks a lot for your help!
>Solution :
Try this:
preg_match('/([\S]+)\-/', $str, $matches);
echo ($matches[1] ?? null);
The \S means "Any non-whitespace character". So it’s kind of like saying "Match one or more non-whitespace characters up to a hyphen."
Test it out at regex101: https://regex101.com/r/E3gOqm/1