Randomimize array inside function PHP











up vote
0
down vote

favorite












please help me.
My function pick up keywords from string(text) and replace them with href tag, that working fine so far, but now I want (because expecting more then one keyword occurrence in string) replace each with random URL address from array, check out code to see what I want and what I get.
Thank you in advance!!



function replacekeywords($str, $search, $url_array) {

$occurrences = substr_count(strtolower($str), strtolower($search));
$newstring = $str;
$match = array();

for ($i=0;$i<$occurrences;$i++) {


// !!!!!!!!!! how do I make $url here randomly based on $url_array?
$url = $url_array[rand(0, count($url_array) - 1)];


$match[$i] = stripos($str, $search, $i);
$match[$i] = substr($str, $match[$i], strlen($search));
$newstring = preg_replace("/b([a-z]*${match[$i]}[a-z]*)b/i","<a href='".$url."' class='link'>$0</a>",strip_tags($newstring,'<br><p>'));
}

return $newstring;
}

$url_array = array('https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3');
$search = 'Whatever text here text also this is about text';
$str = 'text';
$output = replacekeywords($str, $search, $url_array);


Desired output:



Whatever <a href="https://google.com/somelink1">text</a> here <a href="https://google.com/somelink3">text</a> also this is about <a href="https://google.com/somelink2">text</a>


Actual output:



Whatever <a href="https://google.com/somelink2">text</a> here <a href="https://google.com/somelink2">text</a> also this is about <a href="https://google.com/somelink2">text</a>









share|improve this question
























  • Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
    – miken32
    Nov 29 at 21:43















up vote
0
down vote

favorite












please help me.
My function pick up keywords from string(text) and replace them with href tag, that working fine so far, but now I want (because expecting more then one keyword occurrence in string) replace each with random URL address from array, check out code to see what I want and what I get.
Thank you in advance!!



function replacekeywords($str, $search, $url_array) {

$occurrences = substr_count(strtolower($str), strtolower($search));
$newstring = $str;
$match = array();

for ($i=0;$i<$occurrences;$i++) {


// !!!!!!!!!! how do I make $url here randomly based on $url_array?
$url = $url_array[rand(0, count($url_array) - 1)];


$match[$i] = stripos($str, $search, $i);
$match[$i] = substr($str, $match[$i], strlen($search));
$newstring = preg_replace("/b([a-z]*${match[$i]}[a-z]*)b/i","<a href='".$url."' class='link'>$0</a>",strip_tags($newstring,'<br><p>'));
}

return $newstring;
}

$url_array = array('https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3');
$search = 'Whatever text here text also this is about text';
$str = 'text';
$output = replacekeywords($str, $search, $url_array);


Desired output:



Whatever <a href="https://google.com/somelink1">text</a> here <a href="https://google.com/somelink3">text</a> also this is about <a href="https://google.com/somelink2">text</a>


Actual output:



Whatever <a href="https://google.com/somelink2">text</a> here <a href="https://google.com/somelink2">text</a> also this is about <a href="https://google.com/somelink2">text</a>









share|improve this question
























  • Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
    – miken32
    Nov 29 at 21:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











please help me.
My function pick up keywords from string(text) and replace them with href tag, that working fine so far, but now I want (because expecting more then one keyword occurrence in string) replace each with random URL address from array, check out code to see what I want and what I get.
Thank you in advance!!



function replacekeywords($str, $search, $url_array) {

$occurrences = substr_count(strtolower($str), strtolower($search));
$newstring = $str;
$match = array();

for ($i=0;$i<$occurrences;$i++) {


// !!!!!!!!!! how do I make $url here randomly based on $url_array?
$url = $url_array[rand(0, count($url_array) - 1)];


$match[$i] = stripos($str, $search, $i);
$match[$i] = substr($str, $match[$i], strlen($search));
$newstring = preg_replace("/b([a-z]*${match[$i]}[a-z]*)b/i","<a href='".$url."' class='link'>$0</a>",strip_tags($newstring,'<br><p>'));
}

return $newstring;
}

$url_array = array('https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3');
$search = 'Whatever text here text also this is about text';
$str = 'text';
$output = replacekeywords($str, $search, $url_array);


Desired output:



Whatever <a href="https://google.com/somelink1">text</a> here <a href="https://google.com/somelink3">text</a> also this is about <a href="https://google.com/somelink2">text</a>


Actual output:



Whatever <a href="https://google.com/somelink2">text</a> here <a href="https://google.com/somelink2">text</a> also this is about <a href="https://google.com/somelink2">text</a>









share|improve this question















please help me.
My function pick up keywords from string(text) and replace them with href tag, that working fine so far, but now I want (because expecting more then one keyword occurrence in string) replace each with random URL address from array, check out code to see what I want and what I get.
Thank you in advance!!



function replacekeywords($str, $search, $url_array) {

$occurrences = substr_count(strtolower($str), strtolower($search));
$newstring = $str;
$match = array();

for ($i=0;$i<$occurrences;$i++) {


// !!!!!!!!!! how do I make $url here randomly based on $url_array?
$url = $url_array[rand(0, count($url_array) - 1)];


$match[$i] = stripos($str, $search, $i);
$match[$i] = substr($str, $match[$i], strlen($search));
$newstring = preg_replace("/b([a-z]*${match[$i]}[a-z]*)b/i","<a href='".$url."' class='link'>$0</a>",strip_tags($newstring,'<br><p>'));
}

return $newstring;
}

$url_array = array('https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3');
$search = 'Whatever text here text also this is about text';
$str = 'text';
$output = replacekeywords($str, $search, $url_array);


Desired output:



Whatever <a href="https://google.com/somelink1">text</a> here <a href="https://google.com/somelink3">text</a> also this is about <a href="https://google.com/somelink2">text</a>


Actual output:



Whatever <a href="https://google.com/somelink2">text</a> here <a href="https://google.com/somelink2">text</a> also this is about <a href="https://google.com/somelink2">text</a>






php arrays random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 23:13









miken32

22.9k84671




22.9k84671










asked Nov 19 at 22:53









Coxii

256




256












  • Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
    – miken32
    Nov 29 at 21:43


















  • Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
    – miken32
    Nov 29 at 21:43
















Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
– miken32
Nov 29 at 21:43




Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
– miken32
Nov 29 at 21:43












1 Answer
1






active

oldest

votes

















up vote
0
down vote













preg_replace_callback() lets you apply a custom function to each match of a regular expression. It can be used to get the result you're looking for:



<?php
function replacekeywords($needle, $haystack, $urls) {
$needle = preg_quote($needle);
$result = preg_replace_callback(
"/b($needle)b/",
function($matches) use ($urls) {
return $urls[random_int(0, count($urls)-1)];
},
$haystack
);
return $result;
}

$urls = ['https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3'];
$haystack = 'Whatever text here text also this is about text';
$needle = 'text';

echo replacekeywords($needle, $haystack, $urls);


Note of course that there's no allowance here to prevent duplicate entries; it's perfectly likely for at least two or even all three entries to be the same.






share|improve this answer























  • Do you think that there is some way how to appy this in my function? Because my "real world case" is more complex, for example I need to count occurrences.. and no I do not expect more then 3 links, there will be always exactly 3 links. But anyway thank you, will give it try tomorrow.
    – Coxii
    Nov 19 at 23:37










  • Ok now it's a function.
    – miken32
    Nov 19 at 23:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383803%2frandomimize-array-inside-function-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













preg_replace_callback() lets you apply a custom function to each match of a regular expression. It can be used to get the result you're looking for:



<?php
function replacekeywords($needle, $haystack, $urls) {
$needle = preg_quote($needle);
$result = preg_replace_callback(
"/b($needle)b/",
function($matches) use ($urls) {
return $urls[random_int(0, count($urls)-1)];
},
$haystack
);
return $result;
}

$urls = ['https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3'];
$haystack = 'Whatever text here text also this is about text';
$needle = 'text';

echo replacekeywords($needle, $haystack, $urls);


Note of course that there's no allowance here to prevent duplicate entries; it's perfectly likely for at least two or even all three entries to be the same.






share|improve this answer























  • Do you think that there is some way how to appy this in my function? Because my "real world case" is more complex, for example I need to count occurrences.. and no I do not expect more then 3 links, there will be always exactly 3 links. But anyway thank you, will give it try tomorrow.
    – Coxii
    Nov 19 at 23:37










  • Ok now it's a function.
    – miken32
    Nov 19 at 23:46















up vote
0
down vote













preg_replace_callback() lets you apply a custom function to each match of a regular expression. It can be used to get the result you're looking for:



<?php
function replacekeywords($needle, $haystack, $urls) {
$needle = preg_quote($needle);
$result = preg_replace_callback(
"/b($needle)b/",
function($matches) use ($urls) {
return $urls[random_int(0, count($urls)-1)];
},
$haystack
);
return $result;
}

$urls = ['https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3'];
$haystack = 'Whatever text here text also this is about text';
$needle = 'text';

echo replacekeywords($needle, $haystack, $urls);


Note of course that there's no allowance here to prevent duplicate entries; it's perfectly likely for at least two or even all three entries to be the same.






share|improve this answer























  • Do you think that there is some way how to appy this in my function? Because my "real world case" is more complex, for example I need to count occurrences.. and no I do not expect more then 3 links, there will be always exactly 3 links. But anyway thank you, will give it try tomorrow.
    – Coxii
    Nov 19 at 23:37










  • Ok now it's a function.
    – miken32
    Nov 19 at 23:46













up vote
0
down vote










up vote
0
down vote









preg_replace_callback() lets you apply a custom function to each match of a regular expression. It can be used to get the result you're looking for:



<?php
function replacekeywords($needle, $haystack, $urls) {
$needle = preg_quote($needle);
$result = preg_replace_callback(
"/b($needle)b/",
function($matches) use ($urls) {
return $urls[random_int(0, count($urls)-1)];
},
$haystack
);
return $result;
}

$urls = ['https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3'];
$haystack = 'Whatever text here text also this is about text';
$needle = 'text';

echo replacekeywords($needle, $haystack, $urls);


Note of course that there's no allowance here to prevent duplicate entries; it's perfectly likely for at least two or even all three entries to be the same.






share|improve this answer














preg_replace_callback() lets you apply a custom function to each match of a regular expression. It can be used to get the result you're looking for:



<?php
function replacekeywords($needle, $haystack, $urls) {
$needle = preg_quote($needle);
$result = preg_replace_callback(
"/b($needle)b/",
function($matches) use ($urls) {
return $urls[random_int(0, count($urls)-1)];
},
$haystack
);
return $result;
}

$urls = ['https://google.com/somelink1', 'https://google.com/somelink2', 'https://google.com/somelink3'];
$haystack = 'Whatever text here text also this is about text';
$needle = 'text';

echo replacekeywords($needle, $haystack, $urls);


Note of course that there's no allowance here to prevent duplicate entries; it's perfectly likely for at least two or even all three entries to be the same.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 0:00









Peter O.

20.6k95869




20.6k95869










answered Nov 19 at 23:10









miken32

22.9k84671




22.9k84671












  • Do you think that there is some way how to appy this in my function? Because my "real world case" is more complex, for example I need to count occurrences.. and no I do not expect more then 3 links, there will be always exactly 3 links. But anyway thank you, will give it try tomorrow.
    – Coxii
    Nov 19 at 23:37










  • Ok now it's a function.
    – miken32
    Nov 19 at 23:46


















  • Do you think that there is some way how to appy this in my function? Because my "real world case" is more complex, for example I need to count occurrences.. and no I do not expect more then 3 links, there will be always exactly 3 links. But anyway thank you, will give it try tomorrow.
    – Coxii
    Nov 19 at 23:37










  • Ok now it's a function.
    – miken32
    Nov 19 at 23:46
















Do you think that there is some way how to appy this in my function? Because my "real world case" is more complex, for example I need to count occurrences.. and no I do not expect more then 3 links, there will be always exactly 3 links. But anyway thank you, will give it try tomorrow.
– Coxii
Nov 19 at 23:37




Do you think that there is some way how to appy this in my function? Because my "real world case" is more complex, for example I need to count occurrences.. and no I do not expect more then 3 links, there will be always exactly 3 links. But anyway thank you, will give it try tomorrow.
– Coxii
Nov 19 at 23:37












Ok now it's a function.
– miken32
Nov 19 at 23:46




Ok now it's a function.
– miken32
Nov 19 at 23:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383803%2frandomimize-array-inside-function-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen