This worked for me
<?php
$tit = get_the_title( $post->ID );
$arr = explode('|',$tit);
echo $arr[0];
?>
Thread Starter
kdhk
(@kdhk)
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
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];
?>
Totally agree with alchymyth – that’s the part that confused my server as well.
Thread Starter
kdhk
(@kdhk)
I tried with full <?php tag also.It didn’t work.
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
(@kdhk)
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’
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 and post the link here for someone to have a look at it?
Thread Starter
kdhk
(@kdhk)
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
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
(@kdhk)
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