For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2005 > Easy string question...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Easy string question...
Shawn Wilson

2005-11-29, 6:57 pm

This one should be easy, but I'm having trouble finding a one step
solution...

I have a string that contains [blah: <stuff>]

I want to end up with a variable containing just [stuff]

Thanks in advance.

--
Shawn Wilson


Hilarion

2005-11-29, 6:57 pm

> I have a string that contains [blah: <stuff>]
>
> I want to end up with a variable containing just [stuff]



<?php
$some_variable = 'blah: strange stuff';
$other_variable = substr( $some_variable, 6 );
echo $other_variable;
?>

or

<?php
$some_variable = '[blah: strange stuff]';
$other_variable = '[' . substr( $some_variable, 7 );
echo $other_variable;
?>


Hilarion
Shawn Wilson

2005-11-29, 6:57 pm

"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
news:dmi8kd$le7$1@news.onet.pl...
>
>
> <?php
> $some_variable = 'blah: strange stuff';
> $other_variable = substr( $some_variable, 6 );
> echo $other_variable;
> ?>
>
> or
>
> <?php
> $some_variable = '[blah: strange stuff]';
> $other_variable = '[' . substr( $some_variable, 7 );
> echo $other_variable;
> ?>
>
>
> Hilarion


I'm sorry, I wasn't totally clear... I need to use the <> symbols as a
delimiter because I don't know how long either part of the string will be.
Also, I used the [] characters to seperate the variables, that's not part of
their content.

Say this is what I start with:
$var1 = "name: <shawn>";
$var2 = "address: "<123 anywhere>";

I need to end up with this:
$newvar1 = "shawn";
$newvar2 = "123 anywhere";

--
Shawn Wilson


Zilla

2005-11-29, 6:57 pm

Shawn Wilson wrote:
[snip]
> Say this is what I start with:
> $var1 = "name: <shawn>";
> $var2 = "address: "<123 anywhere>";
>
> I need to end up with this:
> $newvar1 = "shawn";
> $newvar2 = "123 anywhere";
>
> --
> Shawn Wilson
>
>


I can't give you a one step solution (maybe others can) but here is one
with two steps:

$var1 = "name: <shawn>;
$var2 = "address: "<123 anywhere>";

$explode1 = explode(": ", $var1);
$explode2 = explode(": ", $var2);

$newvar1 = substr($explode1[1], 1, -1);
$newvar2 = substr($explode2[1], 1, -1);

Zilla
Oli Filth

2005-11-29, 6:57 pm

Shawn Wilson said the following on 29/11/2005 19:38:
> I'm sorry, I wasn't totally clear... I need to use the <> symbols as a
> delimiter because I don't know how long either part of the string will be.
> Also, I used the [] characters to seperate the variables, that's not part of
> their content.
>
> Say this is what I start with:
> $var1 = "name: <shawn>";
> $var2 = "address: "<123 anywhere>";
>
> I need to end up with this:
> $newvar1 = "shawn";
> $newvar2 = "123 anywhere";
>


Regex? e.g.:

preg_match('/<(.*?)>/', $str, $matches);


--
Oli
Shawn Wilson

2005-11-29, 9:55 pm

"Oli Filth" <catch@olifilth.co.uk> wrote in message
news:XS4jf.11650$7p5.4554@newsfe4-win.ntli.net...
> Shawn Wilson said the following on 29/11/2005 19:38:
>
> Regex? e.g.:
>
> preg_match('/<(.*?)>/', $str, $matches);
>
>
> --
> Oli


Thank you, that works perfectly!

--
Shawn Wilson


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com