Title: explode function not working in wordpress loop
Last modified: August 19, 2016

---

# explode function not working in wordpress loop

 *  Resolved [kdhk](https://wordpress.org/support/users/kdhk/)
 * (@kdhk)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/)
 * I want to split the post title when a pipe(|) is encountered.
 *  In this loop
 * <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
 * I tried this code
 * <? $tit = the_title(‘ ‘,’ ‘,0);
 *  $arr = explode(‘|’,$tit);
 *  echo $arr[0];
    ?>
 * The code is not working inside the loop
    Outside the loop it is working fine.
   Pls help

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370626)
 * This worked for me
 *     ```
       <?php
       	$tit = get_the_title( $post->ID );
       	$arr = explode('|',$tit);
       	echo $arr[0];
       ?>
       ```
   
 *  Thread Starter [kdhk](https://wordpress.org/support/users/kdhk/)
 * (@kdhk)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370628)
 * I got the same output with my prev code,it is not splitting the string.
 * For exampl if i hav the ‘post title’ as
 *  ‘motorola placement paper | motorola interview question | motorola interview
   previous papers’
 * i want to retrieve the first part before ‘|’.
    i want the result as ‘motorola
   placement paper’ only.
 * It is working outside
    <?php if(have_posts()) : while(have_posts()) : the_post();?
   > but not within
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370658)
 * try with the full `<?php` tag – not all servers have a version of php that understands
   the shortform.
 *     ```
       <?php $tit = the_title(' ',' ',0);
       $arr = explode('|',$tit);
       echo $arr[0];
       ?>
       ```
   
 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370747)
 * Totally agree with **alchymyth** – that’s the part that confused my server as
   well.
 *  Thread Starter [kdhk](https://wordpress.org/support/users/kdhk/)
 * (@kdhk)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370895)
 * I tried with full <?php tag also.It didn’t work.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370903)
 * check:
    does $tit contain the title? what does the explode return?
 *     ```
       <?php $tit = the_title(' ',' ',0);
       echo $tit;
       $arr = explode('|',$tit);
       print_r($arr);
       echo $arr[0];
       ?>
       ```
   
 *  Thread Starter [kdhk](https://wordpress.org/support/users/kdhk/)
 * (@kdhk)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370908)
 * yes , $tit contains the title.
    the_title(‘ ‘,’ ‘,0); is returing the title
 * but explode worked perfectly if i use the string directly as
    $tit = ‘motorola
   placement paper | motorola interview question’
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370911)
 * more questions than answers from my side – that is all i can think of:
    what 
   is the output of the whole test sequence that i suggested?
 * does explode work with other characters, for instance ‘space’ ?
    `$arr = explode('',
   $tit);`
 * do you think it could be useful if you copy the whole file into a [pastebin ](http://wordpress.pastebin.com/)
   and post the link here for someone to have a look at it?
 *  Thread Starter [kdhk](https://wordpress.org/support/users/kdhk/)
 * (@kdhk)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370912)
 * the output of the whole test sequence that u suggested is the whole title.
 * i can’t test if ‘space’ works bcoz the title returned has the seperator as ‘|’
   by default.
 * as i mentioned before if i try without the_title(”,”,0) i mean if i use string
   directly as
 * $tit = ‘motorola placement paper | motorola interview question papers’
 * it is working perfectly
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370913)
 * last idea: could it be that the ‘|’ character only looks like a | – you know 
   what i mean – but is actually a different character?
    this snippet will output
   the ascii numbers of the characters in the title:
 *     ```
       <?php $tit = the_title(' ',' ',0);
       echo $tit;
       for( $i=0; $i<strlen($tit); $i++) {
       echo ord ( substr($tit, $i, ($i+1)) ).'<br />';
       }
       ?>
       ```
   
 * the ‘|’ should be 124
 *  Thread Starter [kdhk](https://wordpress.org/support/users/kdhk/)
 * (@kdhk)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370915)
 * It worked when i used ascii character of ‘|’ as following
 * <?php $tit = the_title(‘ ‘,’ ‘,0);
    $arr = explode(‘|’,$tit); ?>
 * I tried this previously but i think i use without quotes or instead of
    the_title(‘‘,’‘,
   0); ,i used the_title();
 * Thanks a lot alchymyth

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘explode function not working in wordpress loop’ is closed to new replies.

 * 11 replies
 * 3 participants
 * Last reply from: [kdhk](https://wordpress.org/support/users/kdhk/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/explode-function-not-working-in-wordpress-loop/#post-1370915)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
