Solved

Creat a variable but strip part of it

Posted on 2014-09-09
13
101 Views
Last Modified: 2014-09-12
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
Comment
Question by:rgranlund
  • 6
  • 4
  • 3
13 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Are you still wanting the price bit?

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

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 
LVL 7

Author Comment

by:rgranlund
Comment Utility
Yes that would need to be eliminated also.
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Why not just do

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

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 7

Author Comment

by:rgranlund
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 
LVL 7

Author Comment

by:rgranlund
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
 
LVL 7

Author Closing Comment

by:rgranlund
Comment Utility
This works perfectly and has helped me to better understand.  I getting it slowly.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now