Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How is it possible to choose a specific word/s or number/s from a link with php. I have the following urls where I want to choose only the rss number. Have been trying to use preg_match and preg_replace to no avail unfortunately.
<link><![CDATA[http://www.domain.com/league/news/newsid=21898248704.html?rss=2148704+hakska+iwumao+oioqp+badge+water]]></link>
<link><![CDATA[http://www.domain.com/rugby/video/ball/index.html?rss=2133483+water+none+respective+all+sat's+report]]></link>
As you can see the urls are not the same but both have rss=XXXXXX. My aim is to insert the number after "rss=" into the database.
Would appreciate if anyone can give me a tip of how to do this.
Like this ?
<?php
$link = '<link><![CDATA[http://www.domain.com/league/news/newsid=21898248704.html?rss=2148704+hakska+iwumao+oioqp+badge+water]]></link>';
preg_match('/rss=([0-9]+)/', $link, $matches);
echo $matches[1]; // returns 2148704
?>
$url = "http://www.example.com/foo.php?rss=9001+asdf";
$res = "";
$i = 0;
if (($i = strpos($url, 'rss=')) !== false) {
$res = substr($url, $i + 4); // +4 because the length of 'rss=' is 4, we just want the value
if (($i = strpos($res, '+')) !== false) { // $res contains +, cut that off
$res = substr($res, 0, $i);
}
// res has a value
echo $res;
} else {
// res has no value
}
Example
$link = '';
preg_match('/rss=([0-9]+)/', $link, $matches);
echo $matches[1]; // returns 2148704
Worked fine. Thanks a lot Phantom!
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to obfuscate or cover an email address on PHP.
For this, i have the next code.
<td>
<p class="list-item-heading"><small><?= $row->email ?></small></p>
</td>
Where $row->email is
realname_x123#gmail.com
.
I need to show this as
r*al****_x*2*#gmail.com
I need to replace with * ALWAYS the same parts of the string, not randomly because it will be shown on a list, maximun of 5 or 6 characters visible.
Anytips? I've tried with strpos, and str_replace with no success.
EDIT:
IF this cannot be do. It will be usefull also, for example, to only leave 3 chars from the beggining.
rea***********#gmail.com
I've found a workaround that suits for me.
<?php
function hideEmail($email)
{
$mail_segments = explode("#", $email);
$mail_segments[0] = substr($mail_segments[0], 0, 1) . str_repeat("*", strlen($mail_segments[0]) - 2) . substr($mail_segments[0], -1);
$pos = strpos($mail_segments[1], '.');
$mail_segments[1] = substr($mail_segments[1], 0, 1) . str_repeat("*", strlen($mail_segments[1]) - $pos+1) . substr($mail_segments[1], $pos-1);
return implode("#", $mail_segments);
}
?>
function mailObfuscate($mail) {
//every second character replace with *
$mail = explode('#',$mail);
$array = str_split($mail[0]);
$control = 0;
$ret = '';
foreach ($array as $char) {
if ($control == 0) {$ret .= '*'; $control++;} else {$ret .= $char; $control=0;}
}
return $ret.'#'.$mail[1];
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i'm trying to find an exact word inside a string.
example:
$word = "Many Blocks";
if (strpos($word, "Block")){
echo "You found 1 Block";
}
if (strpos($word, "Blocks")){
echo "You found many Blocks";
}
The problem here is both if are true.. and i need to find only if is the same word..
As Jay Blanchard says you need to do it with regex in following way:--
$word = "Many Blocks";
if ( preg_match("~\bBlocks\b~",$word) )
echo "matched";
else
echo "no match";
Your code would work with an offset on the second search and a few other changes.
$result = 'You found no blocks';
$position = strpos($word, "Block");
if ($position !== false){
$result = "You found 1 Block";
if (strpos($word, "Blocks",$position + 1)){
$result = "You found many Blocks";
}
}
echo $result;
by using the strpos() offset you can keep looping through until the word is no longer found.
$found = 0;
$offset = 0;
while(true){
$position = strpos($word,'Block',$offset );
if ($position === false){break;}
$found++;
$offset = $position + 1; // set offset just beyond the current found word
}
echo "Found: $found";
or
This one the code is simple but is slower:
preg_match_all('/Blocks/',$word,$matches);
$found = count($matches[1]);
You can do it with regex like this:
if(preg_match("/Block(\s|$|\.|\,)/", $string))
This looks for the word "Block" followed by space or dot or comma or end of string.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how do i insert a comma in string like
$str = "0470 06102009 2981485GIR ADE TAUHID";
every length i put in array like
$legth = array(7,8,15,50);
i just wanna make the result like
0470 ,06102009 ,2981485GIR ,ADE TAUHID
where every string splited according to length on the array, including whitespace,
length(7),length(8),length(15),length(50)
how i do that ?
I would do something like this:
$offset = 0;
$result = implode(",",array_map(function($length) use ($str,&$offset) {
$part = substr($str,$offset,$length);
$offset += $length;
return $part;
},$legth));
Demo: http://ideone.com/ISWZuO
Please note that the output of the demo is what you "should" get based on the input you provided. Your input is wrong for the output you say you want - adjust $legth as needed.
if you are sure of the number of spaces and you want to keep them, you can use substr
// substr( your string, start, length )
substr($str , 0, 7)
This will work for you:
$oldString = "0470 06102009 2981485GIR ADE TAUHID";
$newstring = implode(", ", preg_split("/[\s]+/", $oldString));
echo $newstring;
EDIT:
If you need output on the basis of your array. Then I think you need to modify your array first. And apply below code:
$legth = array(7,8,15,50);
$str = "0470 06102009 2981485GIR ADE TAUHID";
$first = 0;
foreach($legth as $l){
echo substr($str , $first, $l)."<br />";
$first = $first + $l;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am using below script for cut my strings.
$titlex = strip_tags($title);
if (strlen($titlex) > 35) {
// truncate string
$stringCutx = substr($titlex, 0, 35);
// make sure it ends in a word so assassinate doesn't become ass...
$titlex = substr($stringCutx, 0, strrpos($stringCutx, ' ')).'...read MOre';
}
but its not work well in all conditions likes if string have 2 white spaces it not work default it some time cut those string or some time not. and if string don't have any white space it not cut string however if it Long storing but its not cut.. I am try to solve this issue but not found any solution because of experience. how i solve this issue ?
I'd go for wordwrap
$titlex = strip_tags($title);
$len=35;
if (strlen($titlex) > $len) {
$stringCutx=explode("\n",wordwrap($titlex,$len,"\n",true),2);
$stringCutx=$stringCutx[0].'...read MOre';
}
$titlex = strip_tags($title);
if (strlen($titlex) > 35) {
$stringCutx = substr($titlex, 0, 35);
if (strlen($stringCutx) >= 35 && isset($title[$titlex])) {
if ($title[$titlex] != ' ') {
$pos = strpos($title, ' ', 35);
if ($pos == 0) {
$res = $text;
} else {
$temp = substr($title, 35, $pos - 35);
$res = $res.$temp.'...read MOre';
}
}
}
}