[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 111
  • Last Modified:

Creat a variable but strip part of it

If I have a variable like this:
$xyz = "Custom Price - My Figure ($494.29)";

Open in new window

But I don't  want to use the entire thing.
So how would I write:
if($xyz = "Custom Price- My Figure %) {$abc = "My Figure";}

Open in new window

So, no matter what comes after "Figure" does not matter.
0
rgranlund
Asked:
rgranlund
  • 6
  • 4
  • 3
1 Solution
 
GaryCommented:
Are you still wanting the price bit?

if(strpos($xyz, "Custom Price - My Figure") !== FALSE){
    $abc = "My Figure";
}

Open in new window

0
 
Ray PaseurCommented:
Just to be clear on the rules...

What is the rule about "Custom Price - "?  Is that always to be eliminated, too?  Or just the part after the word "Figure?"
0
 
rgranlundAuthor Commented:
Yes that would need to be eliminated also.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
GaryCommented:
Why not just do

$abc=str_replace("Custom Price - My Figure ","",$xyz);

Open in new window

0
 
GaryCommented:
I think you are just wanting to get the actual price?

<?php
$xyz = "Custom Price - My Figure ($494.29)";
$price = preg_match('/\((.*?)\)/',$xyz,$matches);
echo $matches[1];

Open in new window

0
 
Ray PaseurCommented:
This sounds like a question with a bit of a subtext.  We have a highly explicit example here, using string literals.  But don't you want some kind of a generalized solution that might work with other data, not just these?  Maybe if you could think in terms of a test-driven development concept we could give you a better solution.  Some of the thought processes are described in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

With the explicit data set, here is the kind of code that would be use to compare and replace the strings.
<?php // demo/temp_rgranlund.php
error_reporting(E_ALL);

// 100% CUT PASTE FROM EE AT http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28514910.html
$xyz = "Custom Price - My Figure ($494.29)";

if ($xyz == "Custom Price - My Figure ($494.29)")
{
    $abc = "My Figure";
}

Open in new window

0
 
rgranlundAuthor Commented:
I have 100% fumbled on this question.  What I want to do is Strip out "Custom - Price" and leave the rest.

if($xyz = "Custom Price- My Figure %)   Sytrip out Custom - Price so that $xyz = "My Figure"

Or if $xyz = "Custom  Price - My Date"  It would leave only My Date.
0
 
Ray PaseurCommented:
The problem we all face in computer programming is that it requires a high level of precision and attention to detail.  Consider this quote:
What I want to do is Strip out "Custom - Price" and leave the rest.
And now look at the test data:
$xyz = "Custom Price - My Figure ($494.29)";
As you can see "Custom - Price" does not appear in the test data, where it says, "Custom Price - " so we still have some confusion about exactly what you want to remove from the original data string.  Maybe you can build on this example.
http://iconoun.com/demo/temp_rgranlund.php
<?php // demo/temp_rgranlund.php
error_reporting(E_ALL);

// 100% CUT PASTE FROM EE AT http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28514910.html
$xyz = "Custom Price - My Figure ($494.29)";

if ($xyz == "Custom Price - My Figure ($494.29)")
{
    $abc = str_replace("Custom Price - ", NULL, $xyz);
}

// SHOW THE ORIGINAL AND WORK PRODUCT
var_dump($xyz, $abc);

Open in new window

0
 
rgranlundAuthor Commented:
Thank you for helping me to this point .  To offer more clarity:

if ($xyz == "Custom Price - My Figure ($494.29)")
{
    $abc = str_replace("Custom Price - ", NULL, $xyz);
}

If $xyz CONTAINS  "Custom Price" remove it and leave everything else.
0
 
Ray PaseurCommented:
This leads us to yet another solution.  They all look fairly close to the same, but I think we may still not have all of the rules and test data that would help find the best solution.  A way of thinking about problems like this is taught in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

<?php // demo/temp_rgranlund.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28514910.html

// THE ONLY TEST DATA WE HAVE
$xyz = "Custom Price - My Figure ($494.29)";

// THE SIGNAL STRING
$sig = "Custom Price";

// THIS STATEMENT ACKNOWLEDGES THAT THE TEST DATA MIGHT NOT CONTAIN THE SIGNAL STRING
$abc = $xyz;

// IF THE SIGNAL STRING IS FOUND IN THE TEST DATA
if (strpos($xyz, $sig) !== FALSE)
{
    $abc = str_replace($sig, NULL, $xyz);
}

// SHOW THE ORIGINAL AND WORK PRODUCT
var_dump($xyz, $abc);

Open in new window

0
 
rgranlundAuthor Commented:
This works perfectly and has helped me to better understand.  I getting it slowly.
0
 
Ray PaseurCommented:
Thanks for the points.  Getting to generalized solutions sometimes takes time and effort (and test cases) but the effort is almost always rewarded with code that runs for years without errors.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now