hi
as i am new to PHP
I am trying to find the name and mobile number from a string.

i am using textarea as input

format is as

1 - name, mobile(10 digit[9999999999], 11 digit[0-9999999999], 12 digit[91-9999999999]) in a single line

2 - user can input only mobile number in a single line

3 - user can enter multiple record (one record in a single line)

Can any one help me?

Thanks in advance.

Recommended Answers

All 5 Replies

use explode()

Member Avatar for diafol

preg_match -search for the start of a number.

>use impload()

I think he means explode().
You can certainly use this: explode on ',' to separate name from telno. Explode on "\n" to separate lines in textbox. You'll need to trim() elements to strip off spaces.

commented: Excellent Bro, Thanks for help +0

its alright i have done it
but my problem is with mobile numbers.

after mobile number there is no ","

and what if any record is not having name ?

Member Avatar for diafol

Personally, I wouldn't have this type of input anyway. I'd have a series of textboxes:

1 textbox for name
1 textbox for mobile with an 'add' button by the side which adds a new textbox for additional mobile numbers etc. This can be done quite easily with Javascript 'append'.

You give the same name attribute for mobile textboxes, e.g. name="mobile[]"
In addition, just for tidiness, move the 'add' button down every time you add a box. Also, you could append a 'remove' button by each textbox.

I'd do all this with the jQuery framework.


The data can be picked up in the php receiving script with something like:

$_POST (single variable)
$_POST (array of values)

You just loop through the array to retrieve all the mobile numbers.

If that ain't enough, I'd have a function run with each 'add textbox' routine that validates the mobile number format.

Just an idea. It compartmentalizes the data, with no messy explode/preg_match etc.

Member Avatar for diafol

Here's what I mean (simplified version):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 	var i = 1;
	var maxNoMobiles = 5;
	$("#addMobile").click(function () {
	if(i>=maxNoMobiles){
            alert("You can only submit up to " + maxNoMobiles + " mobile numbers." );
            return false;
	}
	i++;
	$('<li id="liMob' + i +'"><label for="mobile_' + i + '">Mobile #' + i + ':</label>' + '<input type="text" name="mobile[]" id="mobile_' + i + '" value="" /></li>').appendTo("#ulMobile");
    });
    $("#remMobile").click(function () {
	if(i==1){
          alert("You can't remove the last mobile number!");
          return false;
        }   
        $("#liMob" + i).remove();
	i--;
    });
});
</script>

</head>
<body>

<form id="frmMobiles" name="frmMobiles" method="post" action="http://www.example.com">
	<label for="username">Your name:</label>
	<input type="text" id="username" name="username" value="" />
	<ul id="ulMobile">
		<li>
			<label for="mobile_1">Mobile #1:</label>
			<input type="text" id="mobile_1" name="mobile[]" value="" />
		</li>
	</ul>

	<input type="button" value="Add Another Mobile" id="addMobile" name="addMobile" />
	<input type="button" value="Remove Mobile" id="remMobile" name="remMobile" />
	<input type="submit" value="Accept" id="subMobiles" name="subMobiles" />
</form>

</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.