Solved

STR Replace PHP Wordpress

Posted on 2014-09-15
4
156 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 109

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 109

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 109

Expert Comment

by:Ray Paseur
ID: 40323804
Shining brightly!

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

856 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