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>
php arrays random
add a comment |
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>
php arrays random
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
add a comment |
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>
php arrays random
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
php arrays random
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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