Solved

Creat a variable but strip part of it

Posted on 2014-09-09
13
106 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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
 
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 110

Expert Comment

by:Ray Paseur
ID: 40320058
0
 
LVL 110

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 110

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 110

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 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