Solved

STR Replace PHP Wordpress

Posted on 2014-09-15
4
145 Views
Last Modified: 2014-09-15
How would I take into consideration two different Strings I would like to replace? The following code will remove the words Custom Price - but I would like to add an additional phrase to that, so that it is looking for two different strings.  I have named the second string $sig_alt
								<?php $xyz = WC()->cart->get_item_data( $cart_item );
								// THE SIGNAL STRING
										$sig = "Custom Price -";
										$sig_alt = "Bicycle Info -";
										
										// THIS STATEMENT ACKNOWLEDGES THAT THE TEST DATA MIGHT NOT CONTAIN THE SIGNAL STRING
										$abc = $xyz;
										$abcb = $xyzb;
										// IF THE SIGNAL STRING IS FOUND IN THE TEST DATA
										if (strpos($xyz, $sig) !== FALSE) {
												$abc = str_replace($sig, NULL, $xyz);
										}
echo $abc; 											
								 ?>

Open in new window

0
Comment
Question by:rgranlund
  • 3
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40323489
Generally speaking, you would use one str_replace() function call for each string that you want to replace.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40323503
Hmm... The manual indicates that this would work, too.

<?php 
$xyz = WC()->cart->get_item_data( $cart_item );

// THE ARRAY OF SIGNAL STRINGS
$sig = array
( "Custom Price -"
, "Bicycle Info -"
)
;

// THIS USES AN ARRAY OF "NEEDLES"
$abc = str_replace($sig, NULL, $xyz);
echo $abc;

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 40323679
@ Large Light bulb just turned on inside of my head.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40323804
Shining brightly!

Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

705 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

19 Experts available now in Live!

Get 1:1 Help Now