Solved

str_ireplace - better way to do this?

Posted on 2016-07-28
7
39 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
ID: 41733961
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
ID: 41733970
Sorry $i should be $count just a way of referencing each replacement uniquely
0
 
LVL 22

Assisted Solution

by:Kim Walker
Kim Walker earned 125 total points
ID: 41734094
$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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Accepted Solution

by:
Marco Gasi earned 250 total points
ID: 41734270
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
ID: 41734832
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
ID: 41735102
If you want a loop counter, for() is useful.
0
 

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now