How to validate strings?

Asked

Viewed 262 times

14

I have the following strings:

www.adorocinema.com/filmes/filme-226616/fotos
www.adorocinema.com/slideshows/filmes/slideshow-124792
www.adorocinema.com/noticias/filmes/noticia-125494
www.adorocinema.com/filmes/filme-227703
www.adorocinema.com/noticias/filmes/noticia-113312
www.adorocinema.com/filmes/filme-202686
www.adorocinema.com/filmes/filme-144694

How do I make a check that the result is true for:

www.adorocinema.com/filmes/filme-144694
www.adorocinema.com/filmes/filme-202686
www.adorocinema.com/filmes/filme-227703

and false for:

www.adorocinema.com/filmes/filme-226616/fotos
www.adorocinema.com/slideshows/filmes/slideshow-124792
www.adorocinema.com/noticias/filmes/noticia-125494
www.adorocinema.com/noticias/filmes/noticia-113312

3 answers

19


You can use a fixed part of the string (site address) and dynamic the part of the movie id:

^www\.adorocinema\.com/filmes/filme-\d+$

Says search at the beginning of the line (^) for www followed by a point (\.) note that it must be escaped with bar plus the fixed string and finally one or more digits at the end of the line ($)

$arr = array(
'www.adorocinema.com/filmes/filme-226616/fotos',
'www.adorocinema.com/slideshows/filmes/slideshow-124792',
'www.adorocinema.com/noticias/filmes/noticia-125494',
'www.adorocinema.com/filmes/filme-227703',
'www.adorocinema.com/noticias/filmes/noticia-113312',
'www.adorocinema.com/filmes/filme-202686',
'www.adorocinema.com/filmes/filme-144694');


foreach($arr as $item){
    if(preg_match('#^www\.adorocinema\.com/filmes/filme-\d+$#', $item, $m)){
        echo 'valido: '. $m[0] .'<br>';
    }
}

Exit:

valido: www.adorocinema.com/filmes/filme-227703
valido: www.adorocinema.com/filmes/filme-202686
valido: www.adorocinema.com/filmes/filme-144694

7

You can use the function array_filter to take all values that are compatible with regex.

$arr = array(
    'www.adorocinema.com/filmes/filme-226616/fotos',
    'www.adorocinema.com/slideshows/filmes/slideshow-124792',
    'www.adorocinema.com/noticias/filmes/noticia-125494',
    'www.adorocinema.com/filmes/filme-227703',
    'www.adorocinema.com/noticias/filmes/noticia-113312',
    'www.adorocinema.com/filmes/filme-202686',
    'www.adorocinema.com/filmes/filme-144694'
);

$valids = array_filter($arr, function($item) {
    return preg_match('/filmes\/filme-\d+$/', $item);
});

var_dump($valids);

Exit:

array(3) {
    [3]=> string(39) "www.adorocinema.com/filmes/filme-227703"
    [5]=> string(39) "www.adorocinema.com/filmes/filme-202686"
    [6]=> string(39) "www.adorocinema.com/filmes/filme-144694"
}

Note that I only saw the end of the string, so any link that is not "www.adorocinema.." will also be valid.

See Regex validation in regex101

0

One more way:

<?php
preg_match('/^(?:http(?:s)?[\:][\/][\/](?:www[\.])?adorocinema[\.]com[\/]filmes[\/])(?:filme[\-]([0-9]{6}))$/i', preg_replace('/([\s]+)/i', '', $odominioface), $resultadodominioface);
if((isset($resultadodominioface[1]) == true) and ($resultadodominioface[1] != null)){
echo "Link ok<br />";
echo "O link do filme é: filme-".$resultadodominioface[1]."<br />";
} else{
echo "O link não é válido!<br />";
echo "Nada como retorno!"
}

?>

Now just put an 'array_filter' or a 'foreach' if you are going to parse many links in array form. regex parses the whole url. Accepts the following urls and as many spaces as you want in urls:

http://adorocinema.com/filmes/filme-226616

https://adorocinema.com/filmes/filme-226616

http://www.adorocinema.com/filmes/filme-226616

https://www.adorocinema.com/filmes/filme-226616

Includes a nested 'preg_replace' within the 'preg_match' in order to clear possible spaces in the 'url'. It’s fully functional. I’ve tested it now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.