galayman Posted November 26, 2007 Share Posted November 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 26, 2007 Share Posted November 26, 2007 To get help provide an actual example of what that random data is how in the world is it possible to do this without having a real example? Post the actual data. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 26, 2007 Share Posted November 26, 2007 post sample string and sample output you want and by the way this should be on the regex board Quote Link to comment Share on other sites More sharing options...
galayman Posted November 26, 2007 Author Share Posted November 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 26, 2007 Share Posted November 26, 2007 that will be hard using regex.. just explode your data <? $teng = explode(' ' ,'coffe 34,123 1,220 1,150 1,200 +40'); echo $teng['0'].' price: '.$teng['1'].' volume '.$teng['2'].' high ';//etch.. ?> Quote Link to comment Share on other sites More sharing options...
galayman Posted November 26, 2007 Author Share Posted November 26, 2007 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? Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 26, 2007 Share Posted November 26, 2007 can you tell us how do you get those data? i mean trough file get contents, file database etcc? Quote Link to comment Share on other sites More sharing options...
galayman Posted November 26, 2007 Author Share Posted November 26, 2007 using file_get_content(); then strip_tags(); then using preg_replace(); to remove the data that i do not need. i want to make each line as a different variable in the end. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 26, 2007 Share Posted November 26, 2007 then i guess better if you use file instead of file get contents file returns array in that case data will be easy to manipulate.. Quote Link to comment Share on other sites More sharing options...
galayman Posted November 26, 2007 Author Share Posted November 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2007 Share Posted November 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
galayman Posted November 30, 2007 Author Share Posted November 30, 2007 TOPIC SOLVED! here is the answer http://www.phpfreaks.com/forums/index.php/topic,169786.0.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.