?
Solved

str_ireplace - better way to do this?

Posted on 2016-07-28
7
Medium Priority
?
46 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
[X]
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
7 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 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 500 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1000 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 111

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

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 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