DrZork101
asked on
Detecting and changing strings in PHP
I want to detect the structure of a string and according to how it is structured, change the string.
The four types of structure are as follows:
1 - "Product Name [DVD] [2000]"
2 - "Product Name [DVD]"
3 - "Product Name [2000]"
4 - "Product Name"
And the four transformations would be:
1 - "Product Name [DVD]"
2 - "Product Name [DVD]"
3 - "Product Name" . $variable
3 - "Product Name" . $variable
The part of the string [DVD] will always be in the form [ + any number of characters + ]
The part of the string [2000] aill always be in the form [ + 4 numeric characters + ]
What is the most efficient way to do this - speed is a high priority?
Thanks,
The four types of structure are as follows:
1 - "Product Name [DVD] [2000]"
2 - "Product Name [DVD]"
3 - "Product Name [2000]"
4 - "Product Name"
And the four transformations would be:
1 - "Product Name [DVD]"
2 - "Product Name [DVD]"
3 - "Product Name" . $variable
3 - "Product Name" . $variable
The part of the string [DVD] will always be in the form [ + any number of characters + ]
The part of the string [2000] aill always be in the form [ + 4 numeric characters + ]
What is the most efficient way to do this - speed is a high priority?
Thanks,
The fastest way will be an iterator with simple REGEX. Maybe with switch / case. How does the data arrive? Can you post some sample test data, please? Thanks, ~Ray
ASKER
Sorry about the delay,
Data will come in these forms:
Gladiator [Blu-ray] [2000]
Gladiator [DVD]
Gladiator: Special Anniversary Edition
Braveheart [1995]
And is via an XML feed which is looped over & parsed into an array, per product. I can not change this part of the code as it is already obfuscuted & minimised.
Out of this array i only need name and price which i am getting, however, I need to transform the name to one of the 4 transformations in my first post.
Thanks
Data will come in these forms:
Gladiator [Blu-ray] [2000]
Gladiator [DVD]
Gladiator: Special Anniversary Edition
Braveheart [1995]
And is via an XML feed which is looped over & parsed into an array, per product. I can not change this part of the code as it is already obfuscuted & minimised.
Out of this array i only need name and price which i am getting, however, I need to transform the name to one of the 4 transformations in my first post.
Thanks
Thanks, DrZork101. I think I almost understand the format. It might be easier to discard that obscured code and work with the original XML. If you want to post that, I would be glad to start there. Or in the alternative, can you please show me what the array looks like. You say you need name and price, but I do not see any price in the post above. ?? Also, I'd like to see the expected output from using the data in ID: 25670439; not quite sure I understand how that would compare to the output expected from the original post (where does that "$variable" come from?)
Thanks, ~Ray
Thanks, ~Ray
<?php // RAY_temp_regex_17.php
error_reporting(E_ALL);
/* * COMMETNS FROM THE OP AT EE
* The four types of structure are as follows:
*
* 1 - "Product Name [DVD] [2000]"
* 2 - "Product Name [DVD]"
* 3 - "Product Name [2000]"
* 4 - "Product Name"
*
* And the four transformations would be:
* 1 - "Product Name [DVD]"
* 2 - "Product Name [DVD]"
* 3 - "Product Name" . $variable
* 3 - "Product Name" . $variable // ? SHOULD THIS BE NUMBER 4 ?
*/
// TEST DATA
$arr = array('Gladiator [Blu-ray] [2000]',
'Gladiator [DVD]',
'Gladiator: Special Anniversary Edition',
'Braveheart [1995]',
);
// WHERE TO PUT THE OUTPUT
$out = array();
// NOT SURE WHAT IS EXPECTED IN $variable
$variable = 'VARIABLE';
// ITERATE OVER THE ARRAY
foreach ($arr as $thing)
{
// ELIMINATE ANY BRACKETED 4-DIGIT NUMBER
$thing = preg_replace('/ ?\[[0-9]{4}\]/', '', $thing);
// ADD THE $variable IF PRODUCT NAME IS NOT FOLLOWED BY A BRACKET
if (!strpos($thing, '[')) $thing .= $variable;
// SAVE INTO THE OUTPUT
$out[] = $thing;
}
// SHOW THE WORK
echo "<pre>\n";
print_r($out);
ASKER
Hello Ray,
I was going to post the XML when i got back home from work. Because i think you are right and it would be easier to do things direct from there.
I will also try out that iteration.
Many thanks,
Julian
I was going to post the XML when i got back home from work. Because i think you are right and it would be easier to do things direct from there.
I will also try out that iteration.
Many thanks,
Julian
10-4. I'll be here. Best, ~Ray
ASKER
Hi I have attahced the XML search response - just change the exstension.
The key parts are the individual <item> 's
What I am actually trying to do is get the <Title>, <LowestNewPrice>, & <ProductGroup>
from the XML.
the ProductGroup is the $variable i was referring to in the first post.
So the end result is an array of product items with filtered name & price.
What do you think the best course of action is?
Thanks
productSearchResults.txt
The key parts are the individual <item> 's
What I am actually trying to do is get the <Title>, <LowestNewPrice>, & <ProductGroup>
from the XML.
the ProductGroup is the $variable i was referring to in the first post.
So the end result is an array of product items with filtered name & price.
What do you think the best course of action is?
Thanks
productSearchResults.txt
Not trying to pick on you, but this is not valid XML - it has unescaped apostrophes in it. Where does this come from? Can you get them to fix it so it is standards-compliant? See:
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I understand and will post a new question over there.
The xml is from an Amazon product index which returns 10 products according to a search string. But i can see that there are unescaped 's in it.
Thanks for all your help Ray,
Julian
The xml is from an Amazon product index which returns 10 products according to a search string. But i can see that there are unescaped 's in it.
Thanks for all your help Ray,
Julian
Hi, Julian. I also found the $pound; character in it. There may be something that is missing from the XML declaration that would make this character acceptable, but in the instant case it made the PHP XML processors bark. I'll look for the other question and try to help some more.
Best of luck with it, ~Ray
Best of luck with it, ~Ray
Hmm. Not the "dollar-pound" character. Instead, this: £ - ANSI character number 163