(solved) String Search

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

Post Reply
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

(solved) String Search

Post by davidjwest »

I have a bit of code which allows user to cut and paste spying reports from an online wargame.

The reports are like this:
Espionage report
from 11.08.2005 22:54
Espionage successful!
Offence Units Gard (15:92:11)
Total Losses
Small Warship 1 0
Defence Units ^___^ (11:66:21)
Total Losses
Large Warship 3 0
Small Warship 9 0
Small Merchant Ship 8 0
Archer 45 0
Buildings
Main House Level 10
Gold Mine Level 20
Stone Quarry Level 20
Lumber Mill Level 20
Laboratory Level 15
Watch-Tower Level 8
Research
Spear Level 1
Shield Level 1
Bow Level 10
Catapult Level 10
Sail Level 10
Resources
Gold 2280
Stone 1034
Lumber 3989
Currently I am storing the whole lot as a mediumblob in a mysql database. However to make the output more presentable and to allow extra functions, I need to assign each part of the report to it's own variable, for example I need to assign $gold_mine as 20 and so on.

How would I search for the various keywords such as Gold Mine and assign the numerical value after each one?

Or is there an easier way to do it from the beginning, I did code it originally to make the user enter every single item separately in it's own field but the users don't like the workload! Cutting and pasting the whole lot and letting PHP do the work would be preferable, I assume it can be done?

I did look for a search string function on the PHP site but didn't get too far, I find it's above my level over there!

Thanks for any advice.
Last edited by davidjwest on Thu Sep 01, 2005 2:30 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_replace().. however, if you're breaking the actual message apart and placing variables in it, then you could just store the variables leaving the message in your code. It'd reduce the amount of space the system requires for all these nearly duplicate messages...
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post by davidjwest »

Thanks, I got it working almost, I just need to get the tabs out of the source, I thought this might work but it doesn't seem to.

Code: Select all

<?php
str_replace("\t", "", $description);
?>
What did I do wrong? Can you not replace something with nothing? Is there a remove string function?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can.. you just forgot to store the returned string from str_replace() ;)
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

(solved) Re: String Search

Post by davidjwest »

davidjwest wrote:I have a bit of code which allows user to cut and paste spying reports from an online wargame.

The reports are like this:
Espionage report
from 11.08.2005 22:54
Espionage successful!
Offence Units Gard (15:92:11)
Total Losses
Small Warship 1 0
Defence Units ^___^ (11:66:21)
Total Losses
Large Warship 3 0
Small Warship 9 0
Small Merchant Ship 8 0
Archer 45 0
Buildings
Main House Level 10
Gold Mine Level 20
Stone Quarry Level 20
Lumber Mill Level 20
Laboratory Level 15
Watch-Tower Level 8
Research
Spear Level 1
Shield Level 1
Bow Level 10
Catapult Level 10
Sail Level 10
Resources
Gold 2280
Stone 1034
Lumber 3989
Currently I am storing the whole lot as a mediumblob in a mysql database. However to make the output more presentable and to allow extra functions, I need to assign each part of the report to it's own variable, for example I need to assign $gold_mine as 20 and so on.

How would I search for the various keywords such as Gold Mine and assign the numerical value after each one?

Or is there an easier way to do it from the beginning, I did code it originally to make the user enter every single item separately in it's own field but the users don't like the workload! Cutting and pasting the whole lot and letting PHP do the work would be preferable, I assume it can be done?

I did look for a search string function on the PHP site but didn't get too far, I find it's above my level over there!

Thanks for any advice.
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post by davidjwest »

Thanks again, actually I wont thank you on every thread as it will bump them up and flood the real requests for help, so this is a big thanks for all the people who helped me!
Post Reply