Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creat a variable but strip part of it

Posted on 2014-09-09
13
Medium Priority
?
109 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 111

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 111

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 111

Expert Comment

by:Ray Paseur
ID: 40320058
0
 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

783 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