Looking for a specific pattern in a text variable - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Programming > PHP Development

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-05-2010, 09:54 AM   #1 (permalink)
Junior Member
 
Join Date: Mar 2009
Location: Long Island, NY
Posts: 13
Default Looking for a specific pattern in a text variable

Hi,

I am working on an e-commerce site and I am trying to figure out how to pull the weight out of a huge text string.

example would contain "text string full of mixed stuff weight 34.5 lbs more text full of mixed stuff"

How can I look for the value of the weight in the middle of the text and put it into a variable?

I know I can use the keywords "weight" and "lbs", but am not sure how to pull the value out because it may contain a decimal point.

Thanks
webman2015 is offline   Reply With Quote
Sponsored Links
Old 02-22-2010, 11:02 PM   #2 (permalink)
Junior Member
 
Join Date: Feb 2010
Posts: 6
Default Re: Looking for a specific pattern in a text variable

Just an example of how you could do it don't know if it is of any help.

Code:
<?php
$model='';
$model='Hewlett Packard';
$amount='';
$amount='35.5';
$height='';
$height='2.5';

$string="make|$model|weight|$amount|lbs|height|$height";

$newstring=array();
$newstring = explode('|',$string);

$modeltype='';
$modeltype=$newstring[1];
$weightamount='';
$weightamount=$newstring[3];
$heightamount='';
$heightamount=$newstring[6];

print "model: $modeltype <br />";
print "weight: $weightamount <br />";
print "height: $heightamount";

?>
shadiadiph is offline   Reply With Quote
Old 02-23-2010, 06:18 PM   #3 (permalink)
Junior Member
 
Join Date: Mar 2009
Location: Long Island, NY
Posts: 13
Default Re: Looking for a specific pattern in a text variable

Actually, the text looks more like this in the body of the description:

"Your home baking never tasted so scrumptious! Weight 2.5 lbs. Stoneware. Microwave and dishwasher safe"

The text comes in a spreadsheet from the manufacturer. I need to pull out the actual weight value from the above text. Text will vary from one product to another, but the weight is usually in there somewhere.

Anyone have an idea?
webman2015 is offline   Reply With Quote
Old 02-23-2010, 09:00 PM   #4 (permalink)
Junior Member
 
Join Date: Feb 2010
Posts: 6
Default Re: Looking for a specific pattern in a text variable

something like this?

it extracts 2.5

Code:
<?php
$result=array();
$text='';
$text="Your home baking never tasted so scrumptious! Weight 2.5 lbs. Stoneware. Microwave and dishwasher safe";


$weightvalue='';
$weightvalue= preg_match_all('/[0-9].[0-9]/', $text, $result);

$weightvalue=$result[0][0];

print $weightvalue;
?>
shadiadiph is offline   Reply With Quote
Old 02-24-2010, 12:00 AM   #5 (permalink)
Junior Member
 
Join Date: Mar 2009
Location: Long Island, NY
Posts: 13
Default Re: Looking for a specific pattern in a text variable

Yes, this will help, Thank you.

One more thing is still open though. There are other numbers and patterns in the descriptive text (I just used a small excerpt) and I need to look for the keyword "weight" then use the script you gave me to pull out the actual value right after it.

So, if the keyword "weight" is found, then pull out the numerical value after it. Then I also need to look at the letters immediately following the weight value. I just discovered that it may be measured in "oz", "lbs" or even "grams".

So, here is another example text:

Set includes: ruler, pencils, stapler, staple remover, staple wires, eraser, ball pen, tape, pad, blade, pencil sharpener, scissors, paper clips, push pins, and rotating caddy. Weight 1.2 lbs. Plastic, wood, metal, and paper. 6" x 5 1/4" x 6" high

And sometimes they use "lbs", sometimes "oz", and sometimes "grams". So I need to look for the keyword "weight", pull out the value, then look for a pattern matching either "oz", "lbs", or "grams".

I can use strpos to find the last part, easy enough. if (strpos($text, "lbs"))

How can I look for the keyword "weight", then pull out the value?

Thank you, this is helping a lot so far.
webman2015 is offline   Reply With Quote
Reply

Tags
find string , find value in string , search string

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 05:35 PM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting