Jump to content

[SOLVED] capture a specific string


galayman

Recommended Posts

Hi all, i'm a beginner who love php and have this problem, i'm already download lots of ebook of php to solve this problem, but i think i do not know how to solve this.

This is my problem :

 

I have a data and i put it all together inside a variable called DATA,

this is what inside of $DATA after i gather all the data that i needed.

 

$DATA = "

copyright of me and you 2007 and the others bread 15.22 44.97% crumbs 15.532 6.47% apple 146.932 6.555% coffe 83.72 6.537% thank you all friend and everyone data date and year 2007

";

 

i need to only capture this data and remove the others:

bread 15.22 44.97% crumbs 15.532 6.47% apple 146.932 6.555% coffe 83.72 6.537%

and split it and make each data inside another variable, like this :

 

$a = bread 15.22 44.97%

$b = crumbs 15.532 6.47%

$c = apple 146.932 6.555%

$d = coffe 83.72 6.537%

 

can this be done?

and also the data that i try to capture is random, but always with this type : letter - number - number like bread - 15.22 - 44.97%

 

Thank you for your help and sorry for my english.

 

Best Regards

Link to comment
Share on other sites

Well, if the copyright text is the exact same every time, then you can chop that out of the original data, then explode the variable $DATA using space (" ") as the delimiter. So, let's say you get the copyright and thank you stuff out. We are left with:

 

<?php
$DATA = "bread 15.22 44.97% crumbs 15.532 6.47% apple 146.932 6.555% coffe 83.72 6.537%";
$pieces = explode(" ", $DATA);
$a = $pieces[0] . " " . $pieces[1] . " " . $pieces[2] . " " . $pieces[3];
$b = $pieces[4] . " " . $pieces[5] . " " . $pieces[6] . " " . $pieces[7];
$c = $pieces[8] . " " . $pieces[9] . " " . $pieces[10] . " " . $pieces[11];
$d = $pieces[12] . " " . $pieces[13] . " " . $pieces[14] . " " . $pieces[15];
?>

 

PhREEEk

Link to comment
Share on other sites

Thank you for you reply, it help.

 

may i ask another problem?

 

from this variable :

 

$DATA = "

_SOME OTHER RANDOM DATA_ copyright of me and you 2007 and the others bread 15.22 44.97% crumbs 15.532 6.47% apple 146.932 6.555% coffe 83.72 6.537% thank  you all friend and everyone data date and year 2007 _SOME OTHER RANDOM DATA_

";

 

from the variable, can php search this string bread and remove any character before the bread string, and also search thank  you and remove any character after the thank  you string.

 

so inside the $DATA is always = bread 15.22 44.97% crumbs 15.532 6.47% apple 146.932 6.555% coffe 83.72 6.537% thank you

 

even there is a lot random of data before the string bread and after the thank  you string

 

I hope my question is understandable, still learing english.

 

Best Regards

Link to comment
Share on other sites

Your English is quite good, I understand you perfectly.

 

Yes, PHP can find those string positions and help you remove them. I am going to get some sleep now, so I cannot help with writing that part of the code. There are many here capable of helping you, so be patient and someone will be along.

 

PhREEEk

Link to comment
Share on other sites

simple answer is yes - long answer is yes but what if the preceeding characters containe the same words you are looking for????

 

in order to perform waht you want a regular expression is required - and it needs some rules inorder to work properly...

 

IF the code you want stripping won't contain those words then this should do the trick...

 

<?php
$DATA = " 
_SOME OTHER RANDOM DATA_ copyright of me and you 2007 and the others bread 15.22 44.97% crumbs 15.532 6.47% apple 146.932 6.555% coffe 83.72 6.537% thank  you all friend and everyone data date and year 2007 _SOME OTHER RANDOM DATA_ 
";

$str = preg_repace('/(.*)bread(.*)?thank you(.*)/s','bread$2');

?>

 

have a play with that...

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.