-1

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
6
  • 1
    Concatenating 2 entries is a bad idea in the first place. Commented Oct 28, 2014 at 8:16
  • 3
    pass them seperately or use a concat string/char which id and country must not contain for any condition. i would send them seperately. Commented Oct 28, 2014 at 8:17
  • Has the $id every time a fixed length? Commented Oct 28, 2014 at 8:29
  • stackoverflow.com/questions/4537994/… Commented Oct 28, 2014 at 8:32
  • @EducateYourself Contains your id only numbers? Commented Oct 28, 2014 at 8:45

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

$abc does not contain . (far as we know, so we can't explode by it), he simply concatenated the variables $id and $country with the concatenation keyword . in PHP, which has nothing to do with . being a part of the string.
@Kypros it can be a valid answer if you add a line 'name' => $id.".".$country, though i would not recommend concat as mentioned in my previous comment
@EducateYourself It's iffy and risky. It relies on data and implementation never to change, which you can't always guarantee.
@EducateYourself Because it would make your life (and others probably) easier (no need to concat and then split again), each parameter could have it's own type (integer,string in your case) and you could use/validate them separately, ie isset($_POST['country']){ }
Same thing, what if i have javascript disabled? Your code logic will most probably break. Any particular reason you need to concatenate them? I can't seem to find any real benefits of doing that apart from maybe presentation in some cases
|
0

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.

2 Comments

And can you elaborate to OP what it's suppose to do and how?
@Jonast92 check updated answer, is this what you mean?
0

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.)

6 Comments

it would fail if i live in Guinea-Bissau or Falkland Islands (Malvinas) or Virgin Islands (U.S.) etc for eg
@karanthakkar thanks for notice my! i fixed it! all you examples work now!
not sure about the down voter but nice update ! update output too :P it can't be Switzerland now. jk, cheers ;)
i didnt said it can't work with Switzerland, i just said there was contrast in your code (Virgin Islands (U.S.)) and op (Switzerland). which you corrected afterwords ;)
already done but i would prefer not to concat as you can see in all my comments throughout the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.