Jump to content

Array


unidox

Recommended Posts

I have an array such as:

 

Array

(

    [0] => :

    [1] => 353

    [2] => SGS|Bot-1

    [3] => =

    [4] => #a

    [5] => :SGS|Bot-1

    [6] => +SGS|Bot

    [7] => SGS|Ryan`AFK

    [8] => @a|TaCo

    [9] => SGS|Pat

    [10] => @U|Pat

    [11] => @ChanServ

 

)

 

How do I go through that array, and remove a "+" or a "@" from each index only if it has one in the beginning.

 

Thanks

Link to comment
Share on other sites

If you have any knowledge of regex you may be comfortable with a solution like:

 

foreach ($array as &$value)

{

    $value = preg_replace('/^(@|\+)/', '', $value);

}

 

The characters in red are where you'd need to expand the code (with a pipe character delimiter between them) if you wanted to expand it to remove more characters in future -- the plus is a special character so it must be escaped first with a backslash.

Link to comment
Share on other sites

If you have any knowledge of regex you may be comfortable with a solution like:

 

foreach ($array as &$value)

{

    $value = preg_replace('/^(@|\+)/', '', $value);

}

 

The characters in red are where you'd need to expand the code (with a pipe character delimiter between them) if you wanted to expand it to remove more characters in future -- the plus is a special character so it must be escaped first with a backslash.

 

Why not just?

 

$array = preg_replace('/^(@|\+)/', '', $array);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.