1

I have the following string in PHP:

$cars = "Ford Chevrolet Dodge Chrysler";

I am required to output which words have various properties such as: Which word ends in 'let'? Or which word starts with 'Fo'?

I am unsure how to approach this. I am familiar enough with regex formatting, and I have gotten my preg_match to identify matches, but that's only results given in 1 or 0.

So should I split up the words using the space as a delimiter and search each one for matches individually? Or is there a simpler way of going about this?

I am aware of the preg_split function, but I don't know how to actually access the split strings.

$cars = "Ford Chevrolet Dodge Chrysler";
$param1 = '/(let)+/';
$param2 = '/(fo)+/';

echo preg_match($param1, $cars);
echo preg_match($param2, $cars);

I am aware that this doesn't work, but it prints 1s and verifys to me that matches are being found for both parameters. I just don't know how to extract the full 'Chevrolet' and full 'Ford' for parameter 1 and 2 respectively.

Burninrock24
  • 85
  • 2
  • 8

1 Answers1

3

I am sure you're going to be able to figure out how to insert user input into variables $start or $end – just in case: ask them if they want the input string at the beginning or at the end, and based on the answer, save the string to search in to either $start or $end.

$string = "Ford Chevrolet Dodge Chrysler";
$start = "";
$end = "let";

if(!empty($start))
    preg_match_all("#" . $start . "\w+#", $string, $matches);
elseif(!empty($end))
    preg_match_all("#\w+" . $end ."#", $string, $matches);
else
    die("Nothing has been set!");

print_r($matches);

This is the case implemented for all the words ending with a let in your example string. Bear in mind the regex is only going to match word characters. If you're going to get more complicated strings, maybe another solution should be created.


To simply echo all the matches, use foreach loop like this instead of the print_r function:

if(!empty($matches))
{
    foreach($matches as $match)
        echo $match . "<br>";
}
else
    die("Nothing matched!");
jvitasek
  • 810
  • 8
  • 16
  • Perfect, that works great. So I get the output Array ( [0] => Array ( [0] => Chevrolet ) ) How can I reference the 'Chevrolet' for use in a simple echo statement? – Burninrock24 Mar 26 '15 at 22:46
  • Makes sense. Thank you! Another quick question, what are the # signs in the regex? I understand that the start and end are being concatenated to them, but I don't have experience with those in my limited time with php and regex. – Burninrock24 Mar 26 '15 at 22:53
  • `#` sign is basically the equivalent of `/` at the beginning and the end. `preg_match("#.*#", $string);` is the same as `preg_match("/.*/", $string);`. It just seems more readable to me with the pound signs. – jvitasek Mar 26 '15 at 22:56
  • @Burninrock24 please accept the answer if you're contented with the results. – jvitasek Mar 26 '15 at 22:56
  • The echo statement is just outputting 'Array'. I simply copy and pasted for a test run, is there anything else I should be aware of? edit: Just switched $matches to $matches[0] and it seems to work now. – Burninrock24 Mar 26 '15 at 23:17
  • @Burninrock24 Always `print_r()` your arrays to see their structure while debugging. – jvitasek Mar 26 '15 at 23:20