Solved

Creat a variable but strip part of it

Posted on 2014-09-09
13
102 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
ID: 40313157
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
ID: 40313158
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
ID: 40313168
Yes that would need to be eliminated also.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40313177
Why not just do

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

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40313208
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
ID: 40313214
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Author Comment

by:rgranlund
ID: 40320048
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
ID: 40320058
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40320062
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
ID: 40320112
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
ID: 40320136
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
ID: 40320193
This works perfectly and has helped me to better understand.  I getting it slowly.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40320334
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now