If problems

I’m trying to display either a mobile number or an email address but it only gets the email address or leaves the field blank and I’m not sure why. What it should be doing is if the number starts 07 then it should be displaying the phone number, if not then it should show the email address.

if(substr($tel,2) == '07') { echo fwrite( $fh,"    <PDN>".$tel."</PDN>\r\n" ); } else { echo fwrite( $fh,"    <PDN>".$email."</PDN>\r\n" ); }

Try this:

$tel   = '07123456';
$email = 'jb@gmail.com';
if( '07' === substr($tel,0,2))
{ 
	#echo fwrite( $fh,"    <PDN>".$tel."</PDN>\r\n" ); 
	$str = $tel;
}else{
	$str = $email;
	#echo fwrite( $fh,"    <PDN>".$email."</PDN>\r\n" );
}
echo '<h1>' .$str .'</h1>';

Perfect thank you

Glad I was able to help.

I prefer setting a default and led to believe it is slightly faster.

$tel   = '07123456';
$email = 'jb@gmail.com';

$default = $email; // DEFAULT
if( '07' === substr($tel,0,2))
{ 
	#echo fwrite( $fh,"    &lt;PDN&gt;".$tel."&lt;/PDN&gt;\r\n" ); 
	$default = $tel;
}
echo $default ;

or if you like "One Liners"

$default = ( '07' === substr($tel,0,2) ) ? $tel : $email ;
3 Likes

Thank you, just out of curiosity is it possible to say display &tel if it starts 07 AND is 11 numbers? I just notice that somebody put their number down as 07 ad that cause to outputted file to fail because it has to be 11 numbers starting with 07 or an email.

You could test for str_Len() but what happens if the user inserts spaces or hyphens?

I see what you’re saying, the problem is that the number has to be a continuos number with no spaces or other characters

Try this:

    $number = ' 07-98-76:43-21 0123456'; 
    echo '<br /><br />Before: '.$number;
    
    $bad    = array(' ', '&nbsp;', ':', '-');
    
    $number = str_replace($bad, '', $number);
    echo '<br /><br />After: &nbsp;'.$number;
    
    $number = substr($number,0,11);
    echo '<br /><br />Trunc: '.$number;
    
    if( is_numeric($number) ) 
    {
    echo '<br /><br />is_numeric(): ' .$number;
    }

Or could I use this:

if( '07' === substr($tel,0,2)) || $tel = substr($tel,0,11)
{ 
	echo fwrite( $fh,"    <PDN>".$tel."</PDN>\r\n" ); 
	$str = $tel;
}else{
	$str = $email;
	echo fwrite( $fh,"    <PDN>".$email."</PDN>\r\n" );
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.