php-general Digest 3 Apr 2010 14:29:10 -0000 Issue 6673

Topics (messages 303756 through 303765):

preg_match? Or something else?
        303756 by: Ashley M. Kirchner
        303759 by: Jim Lucas
        303760 by: Nathan Rixham

Re: convert a string into an array
        303757 by: Jim Lucas
        303758 by: Nathan Rixham
        303763 by: Nilesh Govindarajan

array or list of objects of different types
        303761 by: Php Developer
        303762 by: Nathan Rixham
        303764 by: Nilesh Govindarajan

GetElementByClass?
        303765 by: tedd

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
I have an array that's created as follows:

 

$string = "73G    146C   311-     309.1C";

$arr = preg_split("/[\s]+/", $string);

 

Now I need to take each element in that array, and break them up even
further so that I get:

 

73G    => "73" and "G"

146C   => "146" and "C"

311-   => "311" and "-"

309.1C => "309", "1", and "C"  (notice 3 elements here)

 

I'm having a hard time trying to figure out what the proper regex would be
for this, or whether that's the right thing to do.  So far I've gotten this:

 

  preg_match("/^(?P<location>\d+)(?P<letter>[A-Z-])/", $item, $matches);

  print_r($matches);

 

Which gives me:

 

Array

(

    [0] => 73G

    [location] => 73

    [1] => 73

    [letter] => G

    [2] => G

)

Array

(

    [0] => 146C

    [location] => 146

    [1] => 146

    [letter] => C

    [2] => C

)

Array

(

    [0] => 311-

    [location] => 311

    [1] => 311

    [letter] => -

    [2] => -

)

Array

(

)

 

 

However that's as far as it goes.  For the other number it returns an empty
array and I know why, the decimal point.   Now I can evaluate each $item
every time, see if they contain a decimal point, and pass it to a different
regex string, but that seems rather inefficient to me.  So how can I do this
all in one fell swoop?

 

Anyone want to take a stab at it?


--- End Message ---
--- Begin Message ---
Ashley M. Kirchner wrote:
> I have an array that's created as follows:
> 
>  
> 
> $string = "73G    146C   311-     309.1C";
> 
> $arr = preg_split("/[\s]+/", $string);
> 
>  
> 
> Now I need to take each element in that array, and break them up even
> further so that I get:
> 
>  
> 
> 73G    => "73" and "G"
> 
> 146C   => "146" and "C"
> 
> 311-   => "311" and "-"
> 
> 309.1C => "309", "1", and "C"  (notice 3 elements here)
> 
>  
> 
> I'm having a hard time trying to figure out what the proper regex would be
> for this, or whether that's the right thing to do.  So far I've gotten this:
> 
>  
> 
>   preg_match("/^(?P<location>\d+)(?P<letter>[A-Z-])/", $item, $matches);
> 
>   print_r($matches);
> 
>  
> 
> Which gives me:
> 
>  
> 
> Array
> 
> (
> 
>     [0] => 73G
> 
>     [location] => 73
> 
>     [1] => 73
> 
>     [letter] => G
> 
>     [2] => G
> 
> )
> 
> Array
> 
> (
> 
>     [0] => 146C
> 
>     [location] => 146
> 
>     [1] => 146
> 
>     [letter] => C
> 
>     [2] => C
> 
> )
> 
> Array
> 
> (
> 
>     [0] => 311-
> 
>     [location] => 311
> 
>     [1] => 311
> 
>     [letter] => -
> 
>     [2] => -
> 
> )
> 
> Array
> 
> (
> 
> )
> 
> However that's as far as it goes.  For the other number it returns an empty
> array and I know why, the decimal point.   Now I can evaluate each $item
> every time, see if they contain a decimal point, and pass it to a different
> regex string, but that seems rather inefficient to me.  So how can I do this
> all in one fell swoop?
> 
> Anyone want to take a stab at it?
> 
> 

Conditionals are your friend!

<plaintext><?php

$string = "73G    146C   311-     309.1C";

$arr = preg_split("/[\s]+/", $string);

print_r($arr);

foreach ( $arr AS $item ) {
        preg_match('|^(?P<location>\d+)\.?(?P<decimal>\d*)(?P<letter>[A-Z-])|',
                   $item,
                   $matches);

        print_r($matches);
}

?>

-- 
Jim Lucas
NOC Manager
541-323-9113
BendTel, Inc.
http://www.bendtel.com

--- End Message ---
--- Begin Message ---
Jim Lucas wrote:
> Ashley M. Kirchner wrote:
>> I have an array that's created as follows:
>>
>>  
>>
>> $string = "73G    146C   311-     309.1C";
>>
>>
>> Anyone want to take a stab at it?
>>
>>
> 
> Conditionals are your friend!
> 
> <plaintext><?php
> 
> $string = "73G    146C   311-     309.1C";
> 
> $arr = preg_split("/[\s]+/", $string);
> 
> print_r($arr);
> 
> foreach ( $arr AS $item ) {
>       preg_match('|^(?P<location>\d+)\.?(?P<decimal>\d*)(?P<letter>[A-Z-])|',
>                    $item,
>                    $matches);
> 
>       print_r($matches);
> }
> 
> ?>
> 

or w/ preg_match_all:

<?php
$regex = '/(([0-9]+)([^0-9])((?:[0-9]|\s+)))/';
$string = "73G    146C   311-     309.1C";
preg_match_all( $regex , $string , $matches );
print_r( $matches )

--- End Message ---
--- Begin Message ---
Andre Polykanine wrote:
> Hello everyone,
> 
> It's quite simple but I'm still stuck.
> What I need is the following: I have an array as a parameter of my
> custom function. However, I'd like to allow users to enter a string
> instead of an array. In this case (if the parameter is a string), it
> must be replaced with an array containing only one item - actually,
> that string.
> What I'm doing gives me (presumably) errors;
> function Send ($tonames, $toemails, $subject, $message) {
> ...
> if ((!is_array($tonames)) || (!is_array($toemails))) {
> $tonames[]=$tonames;
> $toemails[]=$toemails;
> }
> 
> I can't give the new array a new name since I address it further in a
> loop as my function's parameter... hope you understand what I'm
> saying)
> Thanks!
> 

Do something like this:

$tonames  = (is_array($tonames)  ? $tonames  : array($tonames) );
$toemails = (is_array($toemails) ? $toemails : array($toemails));

-- 
Jim Lucas
NOC Manager
541-323-9113
BendTel, Inc.
http://www.bendtel.com

--- End Message ---
--- Begin Message ---
Andre Polykanine wrote:
> Hello everyone,
> 
> It's quite simple but I'm still stuck.
> What I need is the following: I have an array as a parameter of my
> custom function. However, I'd like to allow users to enter a string
> instead of an array. In this case (if the parameter is a string), it
> must be replaced with an array containing only one item - actually,
> that string.
> What I'm doing gives me (presumably) errors;
> function Send ($tonames, $toemails, $subject, $message) {
> ...
> if ((!is_array($tonames)) || (!is_array($toemails))) {
> $tonames[]=$tonames;
> $toemails[]=$toemails;
> }
> 
> I can't give the new array a new name since I address it further in a
> loop as my function's parameter... hope you understand what I'm
> saying)
> Thanks!
> 

function send( $tonames , $toemails , $subject , $message )
{
  $tonames = (array)$tonames;
  $toemails = (array)$toemails;
}

type juggling is a wonderful thing ;)

--- End Message ---
--- Begin Message ---
On 04/03/10 04:56, Nathan Rixham wrote:
Andre Polykanine wrote:
Hello everyone,

It's quite simple but I'm still stuck.
What I need is the following: I have an array as a parameter of my
custom function. However, I'd like to allow users to enter a string
instead of an array. In this case (if the parameter is a string), it
must be replaced with an array containing only one item - actually,
that string.
What I'm doing gives me (presumably) errors;
function Send ($tonames, $toemails, $subject, $message) {
...
if ((!is_array($tonames)) || (!is_array($toemails))) {
$tonames[]=$tonames;
$toemails[]=$toemails;
}

I can't give the new array a new name since I address it further in a
loop as my function's parameter... hope you understand what I'm
saying)
Thanks!


function send( $tonames , $toemails , $subject , $message )
{
   $tonames = (array)$tonames;
   $toemails = (array)$toemails;
}

type juggling is a wonderful thing ;)


Yeah this i the best. It will allow the users to enter a string or an array without affecting processing of your function.

--
Nilesh Govindarajan
Site & Server Administrator
www.itech7.com
मेरा भारत महान !
मम भारत: महत्तम भवतु !

--- End Message ---
--- Begin Message ---
Hi all,

I want to be able to have an array of elements of different types. As an 
example: the first element is a boolean, the second is an integer, and the 
thirs is a string.

In php there is no typing, i'm just wondering if there is a way to have that, 
it would be a lot better than having an array of strings and have to convert 
each element.

Thank you



      __________________________________________________________________
The new Internet Explorer® 8 - Faster, safer, easier.  Optimized for Yahoo!  
Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/

--- End Message ---
--- Begin Message ---
Php Developer wrote:
> Hi all,
> 
> I want to be able to have an array of elements of different types. As an 
> example: the first element is a boolean, the second is an integer, and the 
> thirs is a string.
> 
> In php there is no typing, i'm just wondering if there is a way to have that, 
> it would be a lot better than having an array of strings and have to convert 
> each element.
> 

var_dump( array( true , 12 , "php already does this" ) );

array(3) {
  [0]=> bool(true)
  [1]=> int(12)
  [2]=> string(21) "php already does this"
}

:)

--- End Message ---
--- Begin Message ---
On 04/03/10 05:42, Nathan Rixham wrote:
Php Developer wrote:
Hi all,

I want to be able to have an array of elements of different types. As an 
example: the first element is a boolean, the second is an integer, and the 
thirs is a string.

In php there is no typing, i'm just wondering if there is a way to have that, 
it would be a lot better than having an array of strings and have to convert 
each element.


var_dump( array( true , 12 , "php already does this" ) );

array(3) {
   [0]=>  bool(true)
   [1]=>  int(12)
   [2]=>  string(21) "php already does this"
}

:)


Yeah. But this feature of PHP is a boon if used carefully and a curse if careless. You can get AMAZING results if you're not careful to check the data types ;)

--
Nilesh Govindarajan
Site & Server Administrator
www.itech7.com
मेरा भारत महान !
मम भारत: महत्तम भवतु !

--- End Message ---
--- Begin Message ---
Hi gang:

Here's the problem.

I have 184 HTML pages in a directory and each page contain a question. The question is noted in the HTML DOM like so:

<p class="question">
  Who is Roger Rabbit?
</p>

My question is -- how can I extract the string "Who is Roger Rabbit?" from each page using php? You see, I want to store the questions in a database without having to re-type, or cut/paste, each one.

Now, I can extract each question by using javascript --

document.getElementById("question").innerHTML;

-- and stepping through each page, but I don't want to use javascript for this.

I have not found/created a working example of this using PHP. I tried using PHP's getElementByID(), but that requires the target file to be valid xml and the string to be contained within an ID and not a class. These pages do not support either requirement.

Additionally, I realize that I can load the files and parse out what is between the <p> tags, but I was hoping for a "GetElementByClass" way to do this.

So, is there one?

Thanks,

tedd
--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--- End Message ---

Reply via email to