/    Sign up×
Community /Pin to ProfileBookmark

Returning multiple expressions from a string?

Hey, I don’t know if I worded that title right.

Basically I need to take a string…

sdfgjsdfg dfsgsd sdfg [sdfgsdfgsdf sdfg] dsfgsdf sdg [dfgd] sdfgsd

And return the stuff in the brackets. Probably to an array since there will be multiple occurances.

It seems like it shouldn’t be too tough but I’m already stuck.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@neilemrichOct 30.2008 — You could try ereg:
[code=php]ereg("([[a-zA-Z ]*])", $string, $return_array)[/code]

String being the string of data and the return_array is the array to put the results into. This is normally used for sticking different matches into an array so I'm not sure what it will do with multiple matches that are the same but it might work!
Copy linkTweet thisAlerts:
@xvszeroauthorOct 30.2008 — Hmm... something like that would probably have worked. One line of code as well.

I did get it working though, just sort of a more convoluted fix. I would do a search for the first [ and the first ] and get their positions in the string, then grab everything in between them, throw it into the array, and define the string as now being equal to everything from one position after the ] to the end. Keep looping until the string has no more [ ] in it.

It's about 15 lines of code where 1 probably works, but whatever... its working correctly.
Copy linkTweet thisAlerts:
@NogDogOct 30.2008 — As the ereg functions are generally considered deprecated in favor of the preg functions, I might suggest:
[code=php]
preg_match('/(?<=[)[^]+](?=])/', $text, $array);
[/code]
×

Success!

Help @xvszero 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...