We help IT Professionals succeed at work.

PHP match only the start of a search

ScottNL1
ScottNL1 asked
on
Making a search system and want to validate the following

the example string:

@sony #3154412 flat screen tv with big remote

When i enter in the text box #315 all the answers need to be with exact match match on the first four characters and open on the end. Same for @so

I ONLY need the match script that returns true or false. If possible provide example without using preg_match. But only if this is possible. Cheers


Comment
Watch Question

Not sure what do you mean by "open"?

But if you'd like to find only if that substring exists within a string, then use strpos:


$searchin = '@sony #3154412 flat screen tv with big remote';
$tofind   = '@sao';
$position = strpos($searchin, $tofind);

if ($position===false) {
	echo 'not found';
} else {
	echo 'found';
}

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.