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
https://forums.phpfreaks.com/topic/211583-array/
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
https://forums.phpfreaks.com/topic/211583-array/#findComment-1103027
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
https://forums.phpfreaks.com/topic/211583-array/#findComment-1103172
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.