Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

str_ireplace - better way to do this?

Posted on 2016-07-28
7
Medium Priority
?
50 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: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 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