Parsing a String of Zip Codes with PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am moving my company from a legacy service to a new system. I have written some conversion utilities in PHP to facilitate the move. One issue I am coming accross is converting the zip codes.
In the old system, Zips could be mixed in one line:
700,701,30518,30511,30000-30010,400,30097,30101-30201
In my new system, each type of zip must be broken into it's own line:
700,701,400
30518,30511,30097
30000-30010,30101-30201
So I need to import the first code block and output the second code block to 3 variables. I am working in PHP. Any Guidance? The order of the zips is random. I'm Lost.

Well, this is what I got:
/(\d+)(-\d+)?/s
/s is to ignore newlines.
<?php
$subject = "700,701,400\n30518,30511,30097\n30000-30010,30101-30201\n";
preg_match_all("/(\d+)(-\d+)?/s", $subject, $matches);
print_r($matches[0]);
?>
Here's output:
Array
(
[0] => 700
[1] => 701
[2] => 400
[3] => 30518
[4] => 30511
[5] => 30097
[6] => 30000-30010
[7] => 30101-30201
)
Alternatively, you can do this:
$arr = explode("\n", $subject);
for ($i = 0; $i < count($arr); $i++) {
preg_match_all("/(\d+)(-\d+)?/s", $arr[$i], $matches);
print_r($matches[0]);
}
If there's only three types, then you can just preg_match for each line.
Output:
Array
(
[0] => 700
[1] => 701
[2] => 400
)
Array
(
[0] => 30518
[1] => 30511
[2] => 30097
)
Array
(
[0] => 30000-30010
[1] => 30101-30201
)
Array
(
)

Related

PHP splitting a URL, delimitted by a '=' [closed]

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
Attempting to split the values after &file= for the following example URL. So i'm trying to extract the number at the end. Any suggestions?
http://blablablabla.com/test.php?type=mp4&tv=&file=3797
you can do it with many ways like using parse_url()
$query = parse_url('http://blablablabla.com/test.php?type=mp4&tv=&file=3797', PHP_URL_QUERY);
parse_str($query, $values);
print_r($values);
output will be
(
[type] => mp4
[tv] =>
[file] => 3797
)
you can also do it with using regex like below
$str="http://blablablabla.com/test.php?type=mp4&tv=&file=3797";
preg_match_all('/([^?&=#]+)=([^&#]*)/',$str,$matched);
print_r($matched);
output will be
(
[0] => Array
(
[0] => type=mp4
[1] => tv=
[2] => file=3797
)
[1] => Array
(
[0] => type
[1] => tv
[2] => file
)
[2] => Array
(
[0] => mp4
[1] =>
[2] => 3797
)
)
You can do this along with the sanitize.
$file=filter_input(INPUT_GET, "file", FILTER_SANITIZE_STRING) // This will get the data and clean the data too.
If you don't need the sanitize, you can simply use it as,
$file=$_GET['file']
I personally recommend the first method.

Parse Email Regex in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have the below string in a file.
BEGIN:VCALENDARPRODID://Yahoo//Calendar
//ENVERSION:2.0METHOD:REQUESTBEGIN:VEVENTSUMMARY:Review MeetingDESCRIPTION:Testing the
yahoomail eventCLASS:PUBLICDTSTART;TZID=Etc/GMT:20140213T163000ZDTEND;TZID=Etc
/GMT:20140213T173000ZLOCATION:ChennaiPRIORITY:0SEQUENCE:1UID:252f25fc-
a39c-4121-9835-774a6c558b33ATTENDEE;RSVP=TRUE:mailto:aaa#aaa.comATTENDEE;
RSVP=TRUE:mailto:bbb#aaa.comATTENDEE;RSVP=TRUE:mailto:xxxin#yahoo.inATTENDEE;
RSVP=TRUE:mailto:test.test#test.orgATTENDEE;RSVP=TRUE:mailto:xxx#xxx.mailgun.orgORGANIZER;
CN=Sherin jemima G;SENT-
BY="mailto:xxxin#yahoo.in":mailto:xxxin#yahoo.inTRANSP:OPAQUEEND:VEVENTBEGIN:VTIMEZONETZID:
Europe/London+0100TZNAME:BSTDTSTART:19810329T010000RRULE:FREQ=YEARLY;BYMONTH=3;
BYDAY=-1SUEND:DAYLIGHTBEGIN:STANDARDTZOFFSETFROM:+0100TZOFFSETTO:+0000TZNAME:GMTDTSTART:199
61027T020000RRULE:FREQ=YEARLY;BYMONTH=10;
BYDAY=-1SUEND:STANDARDBEGIN:STANDARDTZOFFSETFROM:-000115TZOFFSETTO:+0000TZNAME:GMTDTSTART:1
8471201T000000RDATE:18471201T000000END:STANDARDBEGIN:DAYLIGHTTZOFFSETFROM:+0000TZOFFSETTO:+
0100TZNAME:BSTDTSTART:19160521T020000
edit: unnecessary lines cuted.
I have used file_get_contents function to read the string and used preg_match_all function to get the attendees list.
<?php
preg_match_all('/mailto:(.*?)(.com|.org|.net|.in)/', $convert, $emails);
echo "<pre>";
print_r($emails[0]);
echo "</pre>";
?>
Output:
[0] => Array
(
[0] => mailto:aaa#aaa.com
[1] => mailto:bbb#bbb.com
[2] => mailto:xxxin
[3] => mailto:test.test#test.org
[4] => mailto:xxx#xxx.mailgun.org
[5] => mailto:xxxin
[6] => mailto:xxxin
)
Expected Output:
[0] => Array
(
[0] => mailto:aaa#aaa.com
[1] => mailto:bbb#bbb.com
[2] => mailto:xxxin#yahoo.in
[3] => mailto:test.test#test.org
[4] => mailto:xxx#xxx.mailgun.org
[5] => mailto:xxxin#yahoo.in
[6] => mailto:xxxin#yahoo.in
)
Please help me to achieve this.
Escape the dot in the regex:
preg_match_all('/mailto:(.*?)(\.com|\.org|\.net|\.in)/', $convert, $emails);
Your regular expressions is not looking for the # sign.
This already gives better results :
'/mailto:(.*?)#(.*?)(\.com|\.org|\.net|\.in)/'
The fact to list top level domains is not a good idea. There are more than 250 country tlds, and hundreds are coming.
Your file's structure is flawed, it doesn't have a closing delimiter for an email. You have to fix this first. After that it will be possible to write a good regex parser.

foreach php nested array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How foreach this array to < a href="path-to-jpg-file">name< /a>
Array
(
[0] => Array
(
[0] => 01-siena-rosso-new.jpg
[1] => Siena Rosso
)
[1] => Array
(
[0] => 02-siena-noce-new.jpg
[1] => Siena Noce
)
[2] => Array
(
[0] => 03-zloty-dab.jpg
[1] => Złoty Dąb
)
)
Try loop like this:-
foreach($array as $value) {
echo ''.$value[1].'';
}
Working Demo
Expanding on Roopendras, you just need preg_replace:
foreach($array as $value) {
echo ''.$value[1].'';
}

How to compare arrays and objects to find a match PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My first question is, how do I search values of my array inside the objects array [words] and return object if match is found?
How do I search for a better match? In the example below, second object is a better match with 2 words in common, rather than first with only 1 match.
Array
(
[0] => blue
[1] => green
[2] => love
[3] => sandro
)
stdClass Object
(
[1] => stdClass Object
(
[words] => Array
(
[0] => green
[1] => blue
)
[html] => html+img+link+code
)
[2] => stdClass Object
(
[words] => Array
(
[0] => love
[1] => sex
[2] => blue
)
[html] => html+img+link+code
)
)
Code I tried:
foreach ($ads_arr as $ad) {
print_r(array_intersect($ad->words,$words_arr));
}
You can use a forloop for your case, but you should consider defining real php class (not stdClass ) and implement some methods to help you.
foreach($main_std as $id => $sub_std){
$count_match[$id] = 0;
// now, check for each objects
// you can use an other loop with in_array, array_intersect
// or any other way
foreach($the_array as $word_search)
{
// for each word you're looking for, add +1
if (in_array($word_search, $sub_std->words))
$count_match[$id] ++;
}
}
// here, $count_match is an array you can sort by best match or whatever you want
Try out array_intersect() :
$output = array_intersect($array1, $array2);
print_r($output);

how to print values if we have an assoc array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working in codeigniter.
Structure of my array is
Array
(
[0] => stdClass Object
(
[0] => 15
[1] => 14
[2] => 0
[3] => 0
[4] => 0
)
[1] => stdClass Object
(
[0] => 15
[1] => 14
[2] => 0
[3] => 0
[4] => 0
)
)
I want to print the value of arrays.
As soon as they are not arrays but objects, you need to use
$arr[0]->{0}
form.
Use foreach for access all
foreach ($array as $values)
{
foreach ($values as $value)
{
echo $value;
}
}
Or direct access:
$array[0]->{1};
$array[0]->{2};
$array[1]->{5};
etc..
Usage:
$array[ARRAY INDEX]->{OBJECT INDEX};
Suppose your array is in the variable $arr. Then use it:
$i = 0;
foreach($arr as $array){
echo $array[$i];
$i++;
}
OR you can do this:
echo $arr[1]->0; //and so on
You can get data using
$arr[0]->{0}
OR
$arr[0]->{'0'}
stdClass is php's generic empty class, kind of like Object in Java or object in Python. It is useful for anonymous objects, dynamic properties, etc.
So its not array its an object. you will have to use object operator (->)
check below post
stdClass Object foreach PHP

Categories