Solved

If string starts with XXX

Posted on 2014-12-29
6
165 Views
Last Modified: 2014-12-30
Hi

I am outputting two fields from my database... Brand & Title. I am outputting them as below...

<?php echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title']; ?>

Open in new window


My problem is on occasions in my data the title also shows the brand name so the brand name shows twice. The brand name is at the start of the title. How can I check if the start of the title has the brand name and if it does remove it along with the space after it?
0
Comment
Question by:petewinter
  • 3
  • 2
6 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 334 total points
Comment Utility
See if this makes sense:
http://iconoun.com/demo/temp_petewinter.php

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

// MAKE IT EASIER TO READ THE OUTPUTS
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28588117.html
// REF http://php.net/manual/en/function.strpos.php
/* <?php echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title']; ?> */

// TEST CASE
$row_rs_media_new['brand'] = 'Chevrolet';
$row_rs_media_new['title'] = 'Chevrolet Corvette';

// SHOW THE INSTANT CASE
echo 'BEFORE: ';
echo PHP_EOL;
echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title'];
echo PHP_EOL;

// SHOW HOW TO DETECT AND REMOVE THE REDUNDANT INFORMATION WITH PHP STRPOS() FUNCTION
echo 'AFTER USING STRPOS(): ';
echo PHP_EOL;
if (strpos($row_rs_media_new['title'], $row_rs_media_new['brand']) === 0)
{
    echo $row_rs_media_new['title'];
}

Open in new window

0
 
LVL 9

Assisted Solution

by:Brian Tao
Brian Tao earned 166 total points
Comment Utility
This should work:
<?php echo $row_rs_media_new['brand'] . " " . str_replace($row_rs_media_new['brand'] . " " , "", $row_rs_media_new['title']); ?>

Open in new window

0
 

Author Comment

by:petewinter
Comment Utility
Thanks ray so I assume I need an else statement too. Like the below...

if (strpos($row_rs_media_new['title'], $row_rs_media_new['brand']) === 0)
{
    echo $row_rs_media_new['title'];
} else {
    echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title'];
}

Open in new window


taoyipai - Thanks, Your solution works too and is shorter.

Ray / taoyipai - Does it make any difference which option I use. Do they both do the same thing and load just as quickly? Any impacts I need to know about?
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 334 total points
Comment Utility
The overall strategy would be to see if the 'brand' is contained at the beginning of the 'title.'  These are PHP string variables.  PHP strings are documented here:
http://php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.operators.string.php
http://php.net/manual/en/ref.strings.php

Your choice will have no observable effect on performance.  However your choice will depend on the details of the problem you want to solve.  Here are a couple of test cases.  You can install this script on your server exactly as written here and run it to see the results.

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

// MAKE IT EASIER TO READ THE OUTPUTS
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28588117.html
// REF http://php.net/manual/en/function.strpos.php
/* <?php echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title']; ?> */

// TEST CASE
$row_rs_media_new['brand'] = 'Chevrolet';
$row_rs_media_new['title'] = 'Chevrolet Corvette';

// SHOW THE INSTANT CASE
echo 'BEFORE: ';
echo PHP_EOL;
echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title'];
echo PHP_EOL;

// SHOW HOW TO DETECT AND REMOVE THE REDUNDANT INFORMATION WITH PHP STRPOS() FUNCTION
echo 'AFTER USING STRPOS(): ';
echo PHP_EOL;
if (strpos($row_rs_media_new['title'], $row_rs_media_new['brand']) === 0)
{
    echo $row_rs_media_new['title'];
}
?>


<?php

// TEST CASE
$row_rs_media_new['brand'] = 'Chevrolet';
$row_rs_media_new['title'] = 'Chevrolet Corvette';

echo PHP_EOL;
echo $row_rs_media_new['brand'] . " " . str_replace($row_rs_media_new['brand'] . " " , "", $row_rs_media_new['title']);

?>


<?php

// TEST CASE
$row_rs_media_new['brand'] = 'Corv';
$row_rs_media_new['title'] = 'Chevrolet Corvette';

echo PHP_EOL;
echo $row_rs_media_new['brand'] . " " . str_replace($row_rs_media_new['brand'] . " " , "", $row_rs_media_new['title']);

?>

Open in new window

Since we do not have your database we can't really give you a tested-and-working response without some guessing about what should be in the test data set.  If you want to follow the line of thinking that we use in devising test cases, this article may help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

If you're new to PHP and want some good learning resources, this article can help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

And to your question about an else clause - yes you might want to have one of those.  It's worth experimenting with the code and your test data a little bit.  These kinds of things come together very quickly when you can produce some repeatable tests and run a few experiments.
0
 

Author Closing Comment

by:petewinter
Comment Utility
Many thanks for the answer and info.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points!  I love it when great minds come together to solve problems.  Happy New Year to all, ~Ray
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

12 Experts available now in Live!

Get 1:1 Help Now