Good day;
At 08:17 AM 6/5/2001 -0700, Paul wrote:

>--- Nathaniel Mallet <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> >     I'm trying to retrieve a substring from a string, but I'm not
> > sure exactly where and how big that substring is. The substring is
> > delimited by a start and end special character.
>
> >     I thought finding the location of the start and end special
> > characters, then calling substr().
> >
> > And so I have to questions:
> > 1 - How would I find the index for a particular character in a
> > string?

To find the start character use index () i.e.:
$startindex = index ($string, "start_character");

There are two ways you can find your end character. You can continue 
index() i.e.:
$endindex = index ($string, "end_character", $startindex +1);   # the "+1" 
ensures index won't catch your start character (if they happen
                                                                         # 
to be the same character) by accident

or use rindex, which works right to left on your string i.e.
$endindex = rindex ($string, "end_character");

Then to get your substring, use substr() i.e.:

$newline = substr($string, $startindex, ($endindex - $startindex) -1);  # 
in essence, this says take the portion of $string beginning at position 
$startindex 
# and go ($endindex-$startindex) characters. The -1 makes sure everything 
is                                                                    # 
captured you want in your substring. I sometimes have to play around with the
                                                                         # 
+/- 1 in my start and end indexes to get the substring exact, but it should 
work
                                                                         # 
with the length calculation here.

Hope this helps.

Reply via email to