Solved

str_ireplace - better way to do this?

Posted on 2016-07-28
7
44 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 110

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

737 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