# Advanced sub-string

**URL:** <https://forum.kirupa.com/t/advanced-sub-string/261866>\
**Category:** Uncategorized\
**Created:** [June 1, 2008, 1:54am UTC](https://forum.kirupa.com/t/advanced-sub-string/261866 "2008-06-01T01:54:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NeoDreamer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@NeoDreamer](https://forum.kirupa.com/u/NeoDreamer)\
**Post date:** [June 1, 2008, 1:54am UTC](https://forum.kirupa.com/t/advanced-sub-string/261866/1 "2008-06-01T01:54:32Z")

</div>

Given an arbitrary string “abcdefg[hijk]” how can I extract the text before “[” and the text between “[” and “]” ? So I want “abcdefg” stored in some variable and “hjik” stored in another.

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [June 1, 2008, 3:56am UTC](https://forum.kirupa.com/t/advanced-sub-string/261866/2 "2008-06-01T03:56:58Z")

</div>

Either use regular expressions, or a combination of [d-php]strpos[/d-php] and [d-php]substr[/d-php]. Do a strpos() for the first [, and do a substr() to that character, and then do a substr() from the [character to the strpos() of the] character.

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [June 1, 2008, 3:25pm UTC](https://forum.kirupa.com/t/advanced-sub-string/261866/3 "2008-06-01T15:25:26Z")

</div>

the regexp in php might be something like

preg\_match\_all(’/([^[]  
]+)[([^[]  
]+)]/’, $yourString, $resultArray, PREG\_SET\_ORDER);

this will find all instances and put them into the $resultArray array.

each element in the $resultArray will contain another array - [0] = the full string match (eg “abcdefg[hijk]”), [1] = the first part of the string (eg “abcdefg”), and [2] = the bit in the braces (eg “hjik”)

so, for example, for the first match

$resultArray[0][0] = “abcdefg[hijk]”  
$resultArray[0][1] = “abcdefg”  
$resultArray[0][2] = “hijk”

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [June 1, 2008, 3:43pm UTC](https://forum.kirupa.com/t/advanced-sub-string/261866/4 "2008-06-01T15:43:02Z")

</div>

I recommend you use substr and strpos; I suspect it’ll be an order of magnitude faster.

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [June 1, 2008, 4:24pm UTC](https://forum.kirupa.com/t/advanced-sub-string/261866/5 "2008-06-01T16:24:15Z")

</div>

Awww but i just discovered regular expressions and Im all excited about em 😃

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [June 1, 2008, 6:28pm UTC](https://forum.kirupa.com/t/advanced-sub-string/261866/6 "2008-06-01T18:28:17Z")

</div>

Regular expressions are great, but for a task this simple, it’d be silly to use them.
