Tom Tom - 2 months ago 16
PHP Question

Check if string (between // and /) contains a colon and a dot

I have a string that is between

//
and
/
, ie.
{sub1}//{string}/{sub2}


How can I tell (possibly the fastest way) if this string contains both a colon (one or more) and a dot (one or more)? For example:

testsomething//apple:phone./hello -> TRUE (string contains both a colon and a dot)
test1//apple:phone/someth -> FALSE (string doesn't contain a dot)
worl//apple:::ph. one/hello -> TRUE (string contains a colon and a dot)
som//:app.le-phone/thing - TRUE (string contains a colon and a dot)
kor//.apple phone/sor - FALSE (string doesn't contain a colon)
len//:.applephone/fbe - TRUE (string contains a colon and a dot)
bei//apple.phone.:/sse - TRUE (string contains a colon and a dot)
dfsd//apple:phon:e/dsd - FALSE (string doesn't contain a dot)

Answer

pure strpos attempt

$str="worl//apple:::ph. one/hello";

$x=explode('/',$str); //extract your substring, your examples are regular its always $x[2]

$findme   = '.';
$pos = strpos($x[2], $findme);

$findme   = ':';
$pos2 = strpos($x[2], $findme);

if ($pos !== false && $pos2 !== false) {
echo 'its a bingo'; 
}else{
    echo 'no bingo';
}
Comments