0

I have this explode (with list):

$dm = "blablabla.ff";
list($d, $l) = explode('.', $dm, 2);

Now, i want the explode will cut the string only if the string contains . because the list will return error if the string not contains ., error like this: Undefined offset: 1.

How can i do this short way?

  • Possible duplicate of [Check if string contains specific words?](http://stackoverflow.com/questions/4366730/check-if-string-contains-specific-words) – Federkun May 02 '16 at 21:33
  • @Federico No, in addition i search solution for the `Undefined offset: 1` problem. –  May 02 '16 at 21:34
  • this is not a good approach if your wanting a filename extension (which it kind of looks like) –  May 02 '16 at 21:35
  • @Dagon actually i want a domain tld(extension) –  May 02 '16 at 21:37
  • 1
    if you had only asked that in the first place ... –  May 02 '16 at 21:37
  • Possible duplicate of [Getting domain extension from URL?](http://stackoverflow.com/questions/17487559/getting-domain-extension-from-url) – chris85 May 02 '16 at 21:44
  • http://stackoverflow.com/a/6576365/3392762 – Progrock May 02 '16 at 23:20

3 Answers3

2
$dm = "blablabla.ff";
if(strpos($dm,".") !== false){
    list($d, $l) = explode('.', $dm, 2);
}
aprogrammer
  • 1,764
  • 1
  • 10
  • 20
0

If you explode $dm = "blablabla.ff"; you get two arrays. one for $d and one for $l.

$d = 'blablabla';
$l = 'ff';

If you explode $dm = "blablablaff"; you get one array. One for $d and no one for $l.

$d = 'blablablaff';
$l = null;

Now if you have no arrays to fill the list ($l) it will error.

0

You could try this:

    <?php
        $dm     = "blablabla.ff";
        $d      = null;
        $l      = null;

        if( stristr($dm, ".")){
            list($d, $l) = explode('.', $dm, 2);
        }       

        var_dump($d, $l);
Poiz
  • 7,611
  • 2
  • 15
  • 17