I am grabbing the name attribute using jquery and passing it to my Controller via ajax call.
VIEW
<?php
$data = array(
'name' => $id.$country,
'class' => 'send',
'content' => 'Send'
);
echo form_button($data);
?>
JS
var abc = $(self).attr("name");
I would like to know if there is any codeigniter php function which can help me to separate id from country and pass them to my model.
CONTROLLER
$abc = $this->input->post('abc');
$id = first part of abc variable
$country = second part of abc variable
DISCLAIMER:
As #Jonast92 clarified in the comments this answer doesn't solve the solution, and i agree after re-looking at the question since the values do not actually contain a dot and are not in the form of 1.usa but rather 1usa.
Note: I'll leave the initial answer below for reference but this would only work if the characters were separated by a (.) character and not concatenated directly.
Semi-Invalid Answer:
IF the string was concatenated using a . as a separator as in:
'name' => $id . "." . $country,
You could explode that concatenated string into an array using:
$abc = explode(".",$abc);
Then the id will be stored at index 0 and country at index 1:
$id = $abc[0];
$country = $abc[1];
Just make sure that values do not contain any dot (.) characters as that would produce unwanted results.
Note: You can even limit the number of array elements using:
$abc = explode(".",$abc,2);
which will allow countries to still contain a dot without them breaking up further into an array.
Optimal Solution though would be to split those two values and pass them as seperate parameters before even posting them to the php page and only concatenate them if necessary for your application elsewhere.
Can you try this:
$str = $this->input->post('abc');
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
EXAMPLE:
$str = '1654usa';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
To echo out the number use: echo $matches[0][0];
This will return 1654, because the regular expression searches the string for numbers, and will "pull out" all the numbers.
This should work for you if the id only contains numbers!
<?php
$str = '234534534Virgin Islands (U.S.)';
preg_match_all('!\d+!', $str, $matches);
$id = $matches[0][0];
preg_match_all('/((?!\d+).)*$/', $str, $matches);
$country = $matches[0][0];
echo $id . "<br />" . $country;
?>
Output:
234534534
Virgin Islands (U.S.)
Related
I don't know what to call this definition problem. I need to parse value from preg_match_all. before i get the value i want. I need to parse again the multidimensional array value return by preg_match_all.
The point is I need to reuse code with different value. The different only in variable $regex and $match[1][0]; In my database I have 2 columns. Regex value is pattern regex to match. The second column is $match[1][0] variable. What is call the definition?
I don't want to pass value variable $match[1][0]. but the $match[1][0]. and use it later with loop.
<?php
$temporary = "$match[1][0]";
preg_match_all($regex, $source, $match);
$match = $temporary;
echo $match;
You can try eval() function。It run a string with php code!
You can like this
$match = [
1 => [
[1]
]
];
$temporary = '$match[1][0]';
$match = eval('return $match[1][0];');
var_dump($match);
I'm developing an application that takes a full name of a person and then processes it. For example, if the user enters the name like this:
"AlanMichel"
Then the result must be:
"Alan Michel"
I didn't know how I can do that in the php. Anyone can help please?
You can do:
$str = "AlanMichel";
$name = preg_split('/(?=[A-Z])/',$str);
echo implode( " ", $name );
This will result to:
Alan Michel
Try this
function splitAtUpperCase($s) {
return preg_split('/(?=[A-Z])/', $s, -1, PREG_SPLIT_NO_EMPTY);
}
$str = "AlanMichel";
$strArray = splitAtUpperCase($str));
echo $strArray[0]; //first name
echo $strArray[1]; //second name
Output
Alan
Michel
If you don't need the array itself, you can just preprend uppercase characters (except the first) with a space
echo preg_replace('/(?<!^)([A-Z])/', ' \\1', $str);
I have a form and one of the input names has numbers that correspond to information in my database. I need to extract the numbers and set them as variables so I can use them to store and retrieve data from the database.
Example: rdobtn_1_15 or qtybx_9_82
then numbers will be dynamic and will change so I need something that will get the numbers whether they are "20" or "5327"
$input = 'rdobtn_1_15'
$matches = null;
$returnValue = preg_match('/_(\\d+)_(\\d+)/', $input, $matches);
Your matches will be stored as
array (
0 => '_1_15',
1 => '1',
2 => '15',
)
So your numbers are accessable via
echo $matches[1] // 1
echo $matches[2] // 15
For reference: Regular Expressions
You can extract the digits from a string, if this is what you want to achieve, with:
preg_match_all('!\d+!', $your_string, $matches);
print_r($matches);
$array = explode('_', $string);
$element = $array[5]; // or whichever element you want
I want to replace names in a text with a link to there profile.
$text = "text with names in it (John) and Jacob.";
$namesArray("John", "John Plummer", "Jacob", etc...);
$LinksArray("<a href='/john_plom'>%s</a>", "<a href='/john_plom'>%s</a>", "<a href='/jacob_d'>%s</a>", etc..);
//%s shout stay the the same as the input of the $text.
But if necessary a can change de array.
I now use 2 arrays in use str_replace. like this $text = str_replace($namesArray, $linksArray, $text);
but the replace shout work for name with a "dot" or ")" or any thing like that on the end or beginning. How can i get the replace to work on text like this.
The output shout be "text with names in it (<a.....>John</a>) and <a ....>Jacob</a>."
Here is an example for a single name, you would need to repeat this for every element in your array:
$name = "Jacob";
$url = "<a href='/jacob/'>$1</a>";
$text = preg_replace("/\b(".preg_quote($name, "/").")\b/", $url, $text);
Try something like
$name = 'John';
$new_string = preg_replace('/[^ \t]?'.$name.'[^ \t]/', $link, $old_string);
PHP's preg_replace accepts mixed pattern and subject, in other words, you can provide an array of patterns like this and an array of replacements.
Done, and no regex:
$text = "text with names in it (John) and Jacob.";
$name_link = array("John" => "<a href='/john_plom'>",
"Jacob" => "<a href='/jacob'>");
foreach ($name_link as $name => $link) {
$tmp = explode($name, $text);
if (count($tmp) > 1) {
$newtext = array($tmp[0], $link, $name, "</a>",$tmp[1]);
$text = implode($newtext);
}
}
echo $text;
The links will never change for each given input, so I'm not sure whether I understood your question. But I have tested this and it works for the given string. To extend it just add more entries to the $name_link array.
Look for regular expressions. Something like preg_replace().
preg_replace('/\((' . implode('|', $names) . ')\)/', 'link_to_$1', $text);
Note that this solution takes the array of names, not just one name.
I have a string
&168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&
and i have to delete, say, this part &185601651932|mobile|3|120|1& (starting with amp and ending with amp) knowing only the first number up to vertical line (185601651932)
so that in result i would have
&168491968426|mobile|3|100|1&114192088691|mobile|3|555|5&
How could i do that with PHP preg_replace function. The number of line (|) separated values would be always the same, but still, id like to have a flexible pattern, not depending on the number of lines in between the & sign.
Thanks.
P.S. Also, I would be greatful for a link to a good simply written resource relating regular expressions in php. There are plenty of them in google :) but maybe you happen to have a really great link
preg_replace("/&185601651932\\|[^&]+&/", ...)
Generalized,
$i = 185601651932;
preg_replace("/&$i\\|[^&]+&/", ...);
if you want real flexibility, use preg_replace_callback. http://php.net/manual/en/function.preg-replace-callback.php
Important: don't forget to escape your number using preg_quote():
$string = '&168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&';
$number = 185601651932;
if (preg_match('/&' . preg_quote($number, '/') . '.*?&/', $string, $matches)) {
// $matches[0] contains the captured string
}
It seems to me you ought to be using another data structure than a string to manipulate this data.
I'd want this data in a structure like
Array(
[id] => Array(
[field_1] => value_1
[field_2] => value_2
)
)
Your massive string can be massaged into such a structure by doing something like this:
$data_str = '168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&';
$remove_num = '185601651932';
/* Enter a descriptive name for each of the numbers here
- these will be field names in the data structure */
$field_names = array(
'number',
'phone_type',
'some_num1',
'some_num2',
'some_num3'
);
/* split the string into its parts, and place them into the $data array */
$data = array();
$tmp = explode('&', trim($data_str, '&'));
foreach($tmp as $record) {
$fields = explode('|', trim($record, '|'));
$data[$fields[0]] = array_combine($field_names, $fields);
}
echo "<h2>Data structure:</h2><pre>"; print_r($data); echo "</pre>\n";
/* Now to remove our number */
unset($data[$remove_num]);
echo "<h2>Data after removal:</h2><pre>"; print_r($data); echo "</pre>\n";