Solved

str_ireplace - better way to do this?

Posted on 2016-07-28
7
38 Views
Last Modified: 2016-07-30
Hi all,
I have the following snippet of code which I am using to search and highlight keywords in a string (lots of words).

What I really wanted it to do was to also insert an anchor in front of the highlighted match, and then a link that would jump you to the next match. Problem is with my code as it is, it does this per keyword instead of per match. Also wanted to have $count be the total number of replacements made, rather than keywords.

Any ideas of how to fix this? Do I need to dump str_replace and head into regex territory?


$keywordex=array("search","search engine","another","something here");
$count=0;
foreach ($keyword_search as $keywordex) {
	$link=$i+1;
	$html=str_ireplace($keywordex,"<a name='a$i'></a><span class='highlight'>$keywordex</span> <a href='#a$link'>Next</a>",$html);
	++$i;
++$count;
}

Open in new window

0
Comment
Question by:Stephen Forlance
7 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
Comment Utility
What am I missing here? It appears that $i is undefined?

Have you got a complete SSCCE we can look at?
0
 

Author Comment

by:Stephen Forlance
Comment Utility
Sorry $i should be $count just a way of referencing each replacement uniquely
0
 
LVL 21

Assisted Solution

by:Kim Walker
Kim Walker earned 125 total points
Comment Utility
$keyword_search is also undefined. As the subject of the foreach loop, it should be an array.

Also, if $i should be $count then lines 6 and 7 would be redundant.

Plus, the variable $keywordex has different values in and out of the foreach loop scope. Outside the loop it contains an array. But inside the loop it contains the individual elements of the $keyword_search array. If you're trying to iterate through the $keywordex array, your foreach should be
foreach ($keywordex as $keyword_search) {

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Accepted Solution

by:
Marco Gasi earned 250 total points
Comment Utility
So, do you mean this?
$keywordex=array("search","search engine","another","something here");
$count=0;
foreach ($keywordex as $keyword_search) {
	$link=$count+1;
	$html=str_ireplace($keyword_search,"<a name='a$i'></a><span class='highlight'>$keyword_search</span> <a href='#a$link'>Next</a>",$html);
        ++$count;
}

Open in new window

0
 

Author Comment

by:Stephen Forlance
Comment Utility
Not quite, with that the anchor link refers to the place of the keyword in the array, so for example "another" creates links to #a3, no matter how man times its replaced, all "another" links  are #a3

The aim would be to create a link next to each item that is replaced, that when clicked would let the user jump to the next item that was replaced.

The more I think about it the more I think maybe I need to split this into two parts. The first part is the replace.

$html="hello this is a test, search, for items that a search engine would see, with another";
$keywordex=array("search","search engine","another","something here");

foreach ($keywordex as $keyword_search) {
	$html=str_ireplace($keyword_search,"<a name='a$i'></a><span class='highlight'>$keyword_search</span>,$html);
      
}

Open in new window


Once run $html would now have all the replaced terms surrounded by <span class='highlight'>

So the next bit would be be run a Regex that matched

<span class="highlight">(.*?)</span>



What I cant understand is how I would construct the statement so that it would keep count of how many times the replacement loop had run, and therefore assign the correct number to it.


<span class="highlight">(.*?)</span><a name=#$loopnumber><a href="#$loopnumber+1">Next</a>

I was thinking it might have something to do with preg_replace_callback() but I couldnt figure this out....
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
If you want a loop counter, for() is useful.
0
 

Author Closing Comment

by:Stephen Forlance
Comment Utility
Managed to figure it out using the approach I described, and preg call back.
Thanks for the guidance.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

743 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

10 Experts available now in Live!

Get 1:1 Help Now