/    Sign up×
Community /Pin to ProfileBookmark

Help with a simple function

This is how the data is pulled and echoed in the URL:

[code=php]

<? echo $sole[‘newlist’]?>

[/code]

$sole[‘newlist’] is stored in the database as text, like this: name1,name2,name3,name4 (no spaces)

What I want to do is to remove $sender from the echoed $sole[‘newlist’] IF it’s there.
NOTE: the name can appear in the beginning of the list:
[b]jack[/b],jess,martin (has no comma)
and it can appear in the middle or in the end of the list
jess[b],jack[/b],martin (has a comma in front of it)

So if we want to remove jack ($sender] from the echoed $sole[‘newlist’] we would also have to remove the comma that is in front of the name because otherwise it would bug up the system.
Probably have got to do something with array_search, is_array but I couldn’t do anything to solve it…

Any ideas?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@gerald2authorAug 25.2008 — We can just use a new variable for it as well, so I'll change it later myself. like

$newvariable for less hassle...
Copy linkTweet thisAlerts:
@hastxAug 25.2008 — if you explode the list, the comma will be removed automatically:

[code=php]
$newlist_parts = explode(',',$sole['newlist']);
[/code]


Now all the names from newlist are in an array of their own, which you can loop through using foreach...comparing the senders name to the current element. Do you see where this is going? once the names are in their own array, it becomes easy to loop through and compare.

...i guess this could be done with an expression search and/or string replace as well, but if I you are trying to do what i think, this should be an easy method to implement.
Copy linkTweet thisAlerts:
@acorbelliAug 25.2008 — if you explode the list, the comma will be removed automatically:

[code=php]
$newlist_parts = explode(',',$sole['newlist']);
[/code]


Now all the names from newlist are in an array of their own, which you can loop through using foreach...comparing the senders name to the current element. Do you see where this is going? once the names are in their own array, it becomes easy to loop through and compare.

...i guess this could be done with an expression search and/or string replace as well, but if I you are trying to do what i think, this should be an easy method to implement.[/QUOTE]



If he's just trying to remove a name from the list, or swap out names, doing a str_replace() is probably the best bet. It's fewer steps than parsing the array and leaves less room for error where imploding() an array with possibly empty nodes could leave him with a name list like "john,jacob,,shmidt".
Copy linkTweet thisAlerts:
@hastxAug 25.2008 — If he's just trying to remove a name from the list, or swap out names, doing a str_replace() is probably the best bet. It's fewer steps than parsing the array and leaves less room for error where imploding() an array with possibly empty nodes could leave him with a name list like "john,jacob,,shmidt".[/QUOTE]

He wants to remove the comma as well ...which may or may not be there, which will need conditions in addition to str_replace.

I was't considering imploding the array, rather creating a new string from the elements !== $sender which would be fairly easy to control unnecessary commas. more coding, probably. but once the names are in an array you can do more with them ...but maybe I'm just planning ahead, based on the assumptions of what might be intended from the new list.
Copy linkTweet thisAlerts:
@Kyleva2204Aug 25.2008 — [code=php]
$person_to_remove = 'Jack';
$sole['newlist'] = 'Jack,Marry,Person,Person2,etc,Jack,etc2,Jack';
$sole['newlist'] = preg_replace("/($person_to_remove,?)|(,$person_to_remove$)/",'',$sole['newlist']);
echo $sole['newlist'];
[/code]


This should work.
×

Success!

Help @gerald2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...