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;
}
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 5 years ago.
Improve this question
I have string ces123456789101112131415
in the code below. I want to be able to start split from 123456789101112131415 and alter result in index size then try to get the remaining unsplit text in just one line string example expecting 1234 5678 9101 112131415 In result
I have tried:
$code = "ces123456789101112131415";
$start = preg_split("/ces/", $code);
$result = str_split($start[1], 4);
for ($b = 0; $b<3; $b++);
{
echo $result[$b];
}
$total= 0;
$cnt = strlen($result[$b]);
$total += $cnt;
$pay = substr($code,$total);
echo "remain $pay";
but I get 1234 5678 9101 123456789101112131415 as result
Am expecting just 1234 5678 9101 112131415 thanks in advance
This finds ces in string and skips 12 characters, then captures the following digits.
Is that what you are trying to do?
$code = "ces123456789101112131415";
Preg_match("/ces.{12}(\d+)/", $code, $match);
$pay = $match[1];
Echo $pay;
https://3v4l.org/FQ5iX
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
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!
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 a form in my website where users can send messages to multiple recipients, they type in the recipient's numbers separated by commas, I recieve the numbers in my php script as a single post variable, but I need to add the recipient,s area code on front of every number, I have tried to explode the variable using the comma as a separator but I do not know what to do from there
$recipient=$_POST['recipient'];
$numbers=explode(',' $recipient);
for ($i = 0; $i <=sizeof($numbers); $i++) {
}
I do not know how you're getting the area codes in the script, but to prepend the number with the area code, you can just use string concatenation, like so:
$areacode = 123; // this is received dynamically
$result = array(); // initialize empty array
for ($i = 0; $i <= sizeof($numbers); $i++) {
$result[] = $areacode . '_' . $numbers[$i];
}
This can also be done using a foreach:
foreach (explode(',', $recipient) as $number) {
$result[] = $areacode . '_' . $number;
}
Now, if you want to get this back as a comma-separated string, you can use implode(), like so:
$result_string = implode(',', $result);
The short answer is something like:
$numbers_with_area_codes = Array();
for ($i = 0; $i <= sizeof($numbers); $i++) {
$numbers_with_area_codes[] = '+372'.$numbers[$i];
}
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
Suppose I have a string such as this:
ABC.DEF.GHI.JKL (This string could have any length and amount of characters between dots)
I want to add the following combinations into an array.
ABC
ABC.DEF
ABC.DEF.GHI
ABC.DEF.GHI.JKL
So basically the string should be split with the dot character and then the individual sub-strings should be combined.
I have a function already, but in my opinion this seems too complicated to achieve the result.
$myarray = array();
$split = explode('.', 'that.string.with.dots');
$string = '';
for ($i = 0; $i < count($split); $i++) {
$string .= $split[$i];
myarray[] = $string;
$string .= '.';
}
Any suggestions on improving this?
Thanks
Michael
Perhaps this:
$split = explode('.', 'that.string.with.dots');
for ($i = 1; $i < count($split); $i++) {
$split[i] = $split[$i-1] . '.' . $split[i];
}
It just concats the current with the previous.
I have the feeling that you are asking if there is a PHP function that will achieve this result with a single function call, but I don't think there is one.
You can rewrite the code as pritaeas did in his answer, to remove two lines of code, but it will still be the same concept.