Reading parts of a posted variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Reading parts of a posted variable

Post by tristanlee85 »

I'm using $_POST to send form information to another page. An example of what might be received using:

Code: Select all

$destination = $_POST['dest_name'];
could output something like 422 (Syracuse)

What I would like to do is store the first 3 numbers in their own variable. In this case, since $destination echos 422 (Syracuse), how can I stript off the first 3 numbers so I can make a vairable $dest_num echo 422?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read up on regular expressions (the Regex board) .. or maybe use explode().
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you know it will always be three numbers, try substr().
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Worked perfect. Thank you!
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

I'll bring this back since I have a similar question. In phpBB, when you insert information in the signature, it's hashed within the BBcode. For example, if I were to insert an image in my signature, it would say something like

Code: Select all

[img:a86d1f200f]some_image.jpg[/img:a86d1f200f]
What I'm wanting to do is search through the whole signature that is pulled from the database. In other words, this may by my signature code stored in MySQL for phpBB:

Code: Select all

PlastikRacing Owner

[img:a86d1f200f]some_image.jpg[/img:a86d1f200f]

[b:44f2b8ecdc]Image and Video Hosting[/b:44f2b8ecdc]
http://www.plastikhosting.net
What I would like to do is search through the signature and if there is a image inserted using the [IMG] tags, pull it out into it's own variable, kind of like explode(). With this function, the output would be something like:

Code: Select all

$sig[0] = "[img]some_image.jpg[/img]";
$sig[1] = "PlastikRacing Owner" . $sig[0] . "[b]Image and Video Hosting[/b]
http://www.plastikhosting.net";
Maybe I'm going about this the wrong way, but is this possible, or is there an easier way?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

And use preg_match_all()?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Alright. I'm a little confused as to how it works. How do I distinguish between:

Code: Select all

[img:xxxxxxxxxx]
and

Code: Select all

http://www.website.com/image.jpg
and

Code: Select all

[/img:xxxxxxxxxx]
within the signature?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

In the most basic, but with its own caveats:

Code: Select all

#\[(img:[^\]]+)\](.+?)\[/\\1\]#
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Alright, so is there any way for me to echo out what is being matched or not? I'm not having any luck here.

Code: Select all

preg_match('#\[(img:[^\]]+)\](.+?)\[/\\1\]#',$user_sig,$matches);
echo $matches[0];
All I'm trying to do is take the user's signature, $user_sig, and remove any

Code: Select all

[img:xxxxxxxxxx]http://www.website.com/image.jpg[/img:xxxxxxxxxx]
from it, and then store is back as $user_sig.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

try changing regex to following:

Code: Select all

preg_match('#\[(img:[^\]]+)\](.+?)\[/\\1\]#is',$user_sig,$matches);
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Does $matches store the array of what is matched? I think I'm trying to make this more complicated than what it is, or I'm trying to do it the wrong way. Like I stated before, I would like to read $user_sig. If there is anything such as

Code: Select all

[img:xxxxxxxxxx]http://www.website.com/image.gif[/img:xxxxxxxxxx]
within $user_sig, I want to remove it.

If $user_sig =

Code: Select all

PlastikRacing Owner

[img:a86d1f200f]some_image.jpg[/img:a86d1f200f]

[b:44f2b8ecdc]Image and Video Hosting[/b:44f2b8ecdc]
http://www.plastikhosting.net
remove the image so $user_sig =

Code: Select all

PlastikRacing Owner

[b:44f2b8ecdc]Image and Video Hosting[/b:44f2b8ecdc]
http://www.plastikhosting.net
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

I'm making a little bit of progress. I've read over the preg_ functions on PHP.net, but exactly how do you determine something such as

Code: Select all

#\[(img:[^\]]+)\](.+?)\[/\\1\]#is
How about this: with the preg_replace, how could I search for

Code: Select all

<img src="http://www.website.com/images.jpg" alt="Image" title="Image" border="0" />
instead of what I had posted before

Code: Select all

[img:a86d1f200f]some_image.jpg[/img:a86d1f200f]
Post Reply