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
  • 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.
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.


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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 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