How do I achieve the same result using sprintf

Posted on 2011-03-08
Last Modified: 2012-05-11
I am using str_replace to search through a string and strip out a word. But it sems to be slowing down my script.

$result = str_replace('word', '', $example);

Open in new window

I've hear sprint is faster. So how can I acieve the same result using sprintf?

Question by:sparingatom
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
  • 4
  • 3
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35073520
str_replace is not that slow. I suspect you have other problems. Why not post the script here and show us what's happening.

Author Comment

ID: 35073592
The script is fine as it was working well. I added that line of code and the script is noticeably slower.  (the script is processing 1,500+ varaibles) so even if its a little bit slower it makes a big difference.
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35073724
Yes, but it depends WHERE you texted that piece of code. str_replace is not slow. Have you placed inside a loop?

Try using a regex if you want to see a real slowdown.

sprintf can replace known values in a formatted string, but str_replace finds values in unknown locations and replaces them. The two are not really equivalent.
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Author Comment

ID: 35073916
Yes, its inside of a foreach loop.  Not sure what you mean by known values.

I know where in the string the word is i want to srip out its at the end.

The actual code im using looks like this:
	//Strip out titles already containing the word large. 
		$result = str_replace(' large', '', $row['prodname']);

		$entry = array(
			'title' => html_escape($result),
			'link' => html_escape($link),
			'description' => html_escape($desc),
			'g:product_type' => "$row['prodcat"],
			'g:expiration_date' => $expirationDate,
			'g:id' => $row['productid'],
			'g:condition' => html_escape(strtolower($row['prodcondition'])),
			'g:price' => $price

Open in new window

LVL 34

Accepted Solution

Beverley Portlock earned 500 total points
ID: 35074237
What I meant was that with sprintf you take a string like this

"Here is my string and I want my replacement here %s"

So the replacement text is easily found because it has a known marker (the %s) but you are taking a string where the 'markers' (the words to be replaced) need to be located in the string first so sprintf and str_replace are actually very similar and so is their performance.

"I know where in the string the word is i want to srip out its at the end."

Normally for something like this I would recommend a regex, but they are typically 5 to 7 times SLOWER than str_replace.....

Author Closing Comment

ID: 35074845
Not sure why he would want to suggest somthing slower than what I'n already using when I am going to speed.  I appriciate and answer to my question but I didn't like that he suggested somthing is wrong with my code before he answerd the question.
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35081404
"Not sure why he would want to suggest somthing slower than what I'n already using when I am going to speed."

I was not suggesting you use something slower, I was saying that what I would normally recommend is slower than str_replace. You will notice that I did not offer any regex patterns to accomplish the task because it was clear that the performance would be unacceptable.

"I didn't like that he suggested somthing is wrong with my code before he answerd the question"

We rarely see the whole picture when we answer a question and many people leave out details which they consider unnecessary but which may be vital in resolving the issue. In memory operations such as str_replace are the fastest  bit of the PHP/MySQL software so when someone says that using a very, very fast routine is slowing their code it doesn't feel right. Normally in this environment the biggest slowdowns are bad (or no) indexes in MySQL or loops in PHP. That's why I said what I said. Perhaps I should have worded it better.

Look at this PHP page which deals with measuring script execution times. Adapt this and put it in your problem script and measure the time that various stages of the script take. You can even explicitly measure the time taken by str_replace. I think that the results would be very helpful to you in tracking down where the time is being used up.

Featured Post

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 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