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 9 years ago.
Improve this question
I have the following string
$s = "hellomyname";
$result = ...
How can I get the "my" out of this the fastest way possible?
You have to find the position of ((my)) in your string with strpos() function and then fetch the word with substr().
here is an example:
<?php
$s1 = 'laldkfjamydnadjacv zdvzkv';
$pos = strpos($s1, 'my');
echo 'First Position---> ' . $pos;
$my1 = substr($s1, $pos, 2);
echo '<br /> this is result---> ' . $my1;
//second test
$s2 = 'hellomyname';
$pos2 = strpos($s2, 'my');
echo '<br />Second position---> ' . $pos2;
$my2 = substr($s2, $pos2, 2);
echo '<br />this is second result---> ' . $my2;
?>
and this is result:
First Position---> 8
this is result---> my
Second position---> 5
this is second result---> my
I think you're looking for substr($s, 5, 2).
Explode (Delimiter, text)
In your case can use:
$result = explode('my', 'hellomyname'); // array([0] => 'hello', [1] => 'name');
If you need get the last value you can put end():
$result = end(explode('my', 'hellomyname')); //name
Reference
http://www.php.net/manual/pt_BR/function.explode.php
http://us1.php.net/end
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 3 years ago.
Improve this question
I would like to fill this 'A1:F1' out to either an array or a string such that it now equals array {A1, B1, C1, D1, E1, F1} or "A1:B1:C1:D1:E1:F1"
Does anyone have an efficient idea on how to do this?
Thanks.
This is one possible solution. It works in two dimensions. As can be seen in the output, it works column by row, to reverse that simply requires changing the order of the two for loops.
function split_range($range) {
list($start_cell, $end_cell) = explode(':', $range);
if (!preg_match('/^([A-Z]+)(\d+)$/', $start_cell, $start) || !preg_match('/^([A-Z]+)(\d+)$/', $end_cell, $end)) return array();
$cells = array();
for ($c = $start[1]; str_pad($c, strlen($end[1]), " ", STR_PAD_LEFT) <= $end[1]; $c++) {
for ($r = $start[2]; $r <= $end[2]; $r++) {
$cells[] = "$c$r";
}
}
return $cells;
}
echo implode(':', split_range("A1:F1")) . "\n";
echo implode(':', split_range("A1:A6")) . "\n";
echo implode(':', split_range("B2:D4")) . "\n";
echo implode(':', split_range("Q3:AB4")) . "\n";
Output:
A1:B1:C1:D1:E1:F1
A1:A2:A3:A4:A5:A6
B2:B3:B4:C2:C3:C4:D2:D3:D4
Q3:Q4:R3:R4:S3:S4:T3:T4:U3:U4:V3:V4:W3:W4:X3:X4:Y3:Y4:Z3:Z4:AA3:AA4:AB3:AB4
If I don't misunderstood your question, then is one solution to achieve what you said on your question using php. But let me know if I am on wrong track :)
<?php
$str = 'A1:F1';
$arr = explode(":",$str);
$start = $arr[0];
$end = $arr[1];
$range = range($start[0],$end[0]);
$func = function($value) use ($start){
return $value.$start[1];
};
$result = array_map($func,$range);
echo "{".implode(',',$result)."}";
echo PHP_EOL;
echo implode(':',$result);
?>
Output:
{A1,B1,C1,D1,E1,F1}
A1:B1:C1:D1:E1:F1
DEMO: https://3v4l.org/iDsGe
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 6 years ago.
Improve this question
I have strings: 23-65, 123-45, 2-5435, 345-4
I want to add zeros to them so all of them will look like ###-#### (three digits dash four digits): 023-0065, 123-0045, 002-5435, 345-0004
How can i do it in php?
Thanks!
You will need to split them using
$parts = explode('-', $number);`
then use str_pad function:
$parts[0] = str_pad($parts[0], 3, "0");
$parts[1] = str_pad($parts[0], 4, "0");
and then concatenate them again
$number = implode('-', $parts);
Alternatively you can pad them using vsprintf:
$number = vsprintf('%03d-%04d', $parts);
Try:
$str = "23-65, 123-45, 2-5435, 345-4";
$numArray = explode(",",$str);
$str_new = "";
foreach($numArray as $nums) {
$nums = explode("-",$nums);
$num1 = str_pad($nums[0], 3, '0', STR_PAD_LEFT);
$num2 = str_pad($nums[1], 4, '0', STR_PAD_LEFT);
$str_new .= $num1."-".$num2.",";
}
$str_new = rtrim($str_new,",");
Output:
023-0065, 123-0045,0 2-5435, 345-0004
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 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!