/    Sign up×
Community /Pin to ProfileBookmark

How To Dump To Array All Chars Inbetween 2 Chars or Symbols ?

Folks,

Not gonna beat round the bush anymore.
Gonna ask you direct.
How to extract what is inside brackets providing certain chars exist inside the brackets ?

[code]
Bog Off!
[/code]

code]
Get Lost!
[/code]

Look at the two above brackets. Imagine I only want to extract what is inside the brackets if the pattern “g o” exist. If condition met. In this example, everything inbetween

[code] and [/code]

from the first set should be dumped into $extract = array().
How to do this ?
Ok dumping into array not that hard but how to get PHP to:

  • 1. Check what is inbetween two tags or what is inbetween two chars and

  • 2. How to extract all data from within the two tags.Opening tag and closing tag. Or two chars ?
  • Imagine, I got this string:

    >

    Gonna kick your backside to Kingdom Come you King Kong!

    Now imagine, I want to dump into array all chars that are in-between the “Gonna” and “to” (opening word and closing word). Which PHP function to use ? Don’t tell me Knees to use a bunch of functions more than one. substring, implode, explode, str_pos etc. Too much headache to do things the long way.

    to post a comment
    PHP

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @SempervivumFeb 11.2021 — My solution:
    $str = 'Gonna kick your backside to Kingdom Come you King Kong!';
    $result = preg_match('/Gonna(.+)to/', $str, $matches);
    var_dump($matches);
    $arr = str_split($matches[1]);
    var_dump($arr);
    I was not able to do it with one function.
    Copy linkTweet thisAlerts:
    @NogDogFeb 12.2021 — If I'm understanding correctly, and if there could be multiple matches within the string, I suppose you could do something like:
    <i>
    </i>$extract = array();
    $test = "This is just a test of PHP. It is only a test.";
    // match between "is " and " test":
    preg_match_all('/bis (.+?) testb/i', $test, $matches);
    $extract = array_merge($extract, $matches[1]);

    Dumping $extract then gives you:
    <i>
    </i>array (
    0 =&gt; 'just a',
    1 =&gt; 'only a',
    )

    Hey, it's a slow evening. But then this opens up the possibility of having to explain regular expressions (regex), which might be more confusing than OOP.
    ×

    Success!

    Help @developer_web 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.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: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

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

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