Jump to content

[SOLVED] string manipulation using regex


galayman

Recommended Posts

Hi, all. This question is about regex

i have this string that i get it from some site

"_some_other_random_data_ coffe 34,123 _some_other_random_data_"

 

and i like to change the sting to

"_some_other_random_data_ coffe price: 34,123 _some_other_random_data_"

 

to find this string i think this regex will work

[a-zA-z][ ][0-9]

 

because when i try to manipulate it using preg_replace("/[a-zA-z][ ][0-9]/, "", $data);

this string "coffe 34,123" dissapeared

 

but how to change the space between the letter and decimal to "price: "

so the result will be like 

"_some_other_random_data_ coffe price: 34,123 _some_other_random_data_"

 

maybe there is a fuction in php that i can use perhaps?

 

Best Regards

Link to comment
Share on other sites

i'm sorry,

 

this is the data

"

coffe 34,123 1,220 1,150 1,200 +40

aplle 3,690 680 660 660 -10

grape 44,255 305 295 300 -10

melon 0 --- --- 195 ...

pineapple 0 --- --- 710 ...

banana 1,249,160 4,650 4,400 4,650 +300

"

 

in the end i want to make the data look like this

"

coffe price : 34,123 volume : 1,220 high : 1,150 low : 1,200 up :+40

"

 

if i know how to insert a string using regex, i think i can continue the rest using the same fuction

 

Best Regard

Link to comment
Share on other sites

sorry, i'm not aware there is another forum about regex, this sub forum is the best place for me, so i'm not looking around, sorry, but when the problem solved, maybe mod can move this topic?

 

well, the data that i collected is huge, maybe around 1000 line, by exploded it maybe my code will be huge too, maybe if i'm using regex the code will be much more simpler?

Link to comment
Share on other sites

well, i think i to make it to array i can use explode, but right now i need the data in string format. is there a way to change the

"coffe 34,123 1,220 1,150 1,200 +40"

to

"coffe price : 34,123 volume : 1,220 high : 1,150 low : 1,200 up :+40"

using regex to add the "price" string?

i only need to add a string between the letter and decimal, and i'm planning to use it in large number of line, maybe regex can help me.

 

Best regards

Link to comment
Share on other sites

try this

 

$str = "your data here...";

$str = preg_replace("/coffe ([\d\,]+)/g","coffe price : $1",$str);
echo $str;

 

but I don't understand why you want to use regexp you can just use str_replace

 

$str = "your data here...";

$str = str_replace("coffe","coffe price :",$str);
echo $str;

 

hope its helpful

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.