Page 1 of 1

(solved) String Search

Posted: Sun Aug 14, 2005 3:13 pm
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.

Posted: Sun Aug 14, 2005 7:49 pm
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...

Posted: Tue Aug 16, 2005 6:11 pm
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?

Posted: Tue Aug 16, 2005 7:00 pm
by feyd
you can.. you just forgot to store the returned string from str_replace() ;)

(solved) Re: String Search

Posted: Thu Sep 01, 2005 2:29 pm
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.

Posted: Thu Sep 01, 2005 2:31 pm
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!