Hi Everyone,
How can I add a + sign to the following string.... for some reason I just can not figure it out
I have tried ".+
and ".+."
and sign quotes - both options but it just doesnt seem to work for me....

$number = $Code."".substr( $mobnum, 0 );

So, number will be the international code of the country plus the sms text message
for example 4401234567890 for UK numbers
but for international numbers I am trying to add the + sign
So it reads +11234567890 for the US...

Hope someone can help & explain where I am going wrong
Thanks

Dani AI

Generated

As and noted, a leading plus is just a character prefix and will fix simple cases. For reliable handling, it helps to normalize the input first (strip spaces, punctuation, convert a leading "00" to "+") and only add a plus when the number doesn’t already include one. The suggestion from to use "{+}" will literally insert braces and is not correct.

A compact workflow that avoids common pitfalls (treat phone numbers as strings, remove unwanted characters, avoid duplicate country codes) looks like this:

$country = $Code;              // e.g. "1" or "44"
$input   = trim($mobnum);

// convert leading "00" to "+"
if (strpos($input, '00') === 0) {
    $input = '+' . substr($input, 2);
}

// keep digits and an initial plus only
$clean = preg_replace('/[^+\d]/', '', $input);

if (strpos($clean, '+') !== 0) {
    $digits = preg_replace('/\D+/', '', $clean);
    if (strpos($digits, $country) === 0) {
        $clean = '+' . $digits;
    } else {
        $clean = '+' . $country . ltrim($digits, '0');
    }
}

$formattedNumber = $clean;

Notes and cautions: do not cast phone numbers to integers (leading zeros and large numbers will be lost). When embedding the number in a URL or form, encode it (e.g., rawurlencode) so the plus sign isn’t misinterpreted. Some SMS gateways accept "00" instead of "+", so check the target API and convert accordingly. Finally, substr($mobnum, 0) simply returns the whole string and is unnecessary in this context.

Recommended Answers

All 3 Replies

You should just be able to do this:

$number = '+' . $Code."".substr( $mobnum, 0 );

If you're just looking to add a plus sign at the beginning of a sequence of numbers, then it's as simple as the following:

$number = 1234567890;
$complete_number = "+" . $number;

Just ensure the + sign is inside the quotation marks.

Sorry if that's not what you're looking for, it's hard to tell from your code since I don't know the values of the variables.

Member Avatar for Member #949455

Hope someone can help & explain where I am going wrong

You didn't do anything wrong. It's just that + sign is to used for international call. It's pretty expensive.

Instead of this ".+ or this ".+." used "{+}"

Try this:

    $number = $Code."{+}".substr( $mobnum, 0 );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.