php-general Digest 19 Dec 2010 06:55:33 -0000 Issue 7093

Topics (messages 310127 through 310137):

Hot to replace words with preg_replace without duplicates
        310127 by: Merlin Morgenstern

Re: HTML id attribute and arrays
        310128 by: Jim Lucas

Re: Problems w/ goto
        310129 by: David Hutto
        310130 by: David Hutto
        310131 by: David Hutto
        310132 by: David Hutto

Re: array question
        310133 by: Jim Lucas

PHPmailer.. best way to send to many recipients?
        310134 by: Govinda

goto - My comments
        310135 by: Ethan Rosenberg
        310136 by: Robert Cummings
        310137 by: Thomas Anderson

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Hi there,

I want to create a function which will replace certain words out of a text with internal links. That works so far, but if I have two matches, I end up with invalid html code.

Example:
Welpen                  /hunde
Chihuahua Welpen        /hunde,chihuahua

function seo_internal_links($str, $links, $limit) {
        foreach($links AS $link){
                $pattern[$k] = "~\b($link[phrase])\b~i";
                $replace[$k] = '<a href="'.$link[link].'">\\1</a>';
                $k++;
        }
        return preg_replace($pattern,$replace,$str, $limit);
}

seo_internal_links($ad[text], $seo_internal_links, $limit = 1);

This will result in:
<a href="//hunde,chihuahua">Chihuahua <a href="/hunde">Welpen</a></a>

Has somebody an idea on how to avoid this? I would also like to limit the amount of hits, but the limit in preg_replace accounts only for unique words, not the whole array.

Thank you for any hint,

Merlin

--- End Message ---
--- Begin Message ---
What are you trying to accomplish?

What do you think it wrong with the code below?

On 12/17/2010 8:19 AM, Martin C wrote:
Hi,

PHP converts x[a]=b parameter of the HTTP request as an array named x
with its item named a set to value b. So, it seems possible to have the
following (X)HTML code:
<input type="text" name="x[a]" id="x[a]" value="b" />
Unfortunatelly, HTML specification does not allow neither "[" nor "]"
inside the id attribute. Specifically:
* Must begin with a letter A-Z or a-z
* Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".")
* Values are case-sensitive

How should I follow the HTML specification while having the passed
parameters automatically converted to arrays in PHP?

Thank you for any tips,

Martin



--- End Message ---
--- Begin Message ---
On Sat, Dec 18, 2010 at 12:45 PM, Geoffrey Bernardo Van Wyk
<[email protected]> wrote:
> Ethan,
>
> I tried to test your code, but I get this error next to the labels and goto
> statements:

As a 'professional' programmer, working for an entity, we deal with
these problems as we go.  As novices, we deal with it on a daily
basis. As computer scientists, we think of it as a problem already
solved, but in need of translation. Understand, buddy pal.


>
> Language feature not compatible with PHP version indicated in project
> settings
>
> I have PHP 5.3.0.
>
> Geoffrey
>
>  _____
>
> From: Ethan Rosenberg [mailto:[email protected]]
> Sent: 17 December 2010 06:39 PM
> To: php-db-lists.php.net; [email protected]
> Subject: [PHP] Problems w/ goto
>
>
>
> Dear List -
>
> I am sending this again since it does not seem to have posted.
>
> Ethan
> +++++++
> Dear List -
>
> Thank you with your excellent help in the past.  Here is another puzzler....
>
>
> I am trying to write a program that can have two(2) independent forms
> in one PHP file.  When I run the code below [from PHP - A Beginner's
> Guide], to which I have added a second form, it freezes.  Without the
> goto statements, it runs.  When it does run, it displays both forms
> on one Web screen. What I desire is for the first form to be
> displayed, the data entered and then the second form displayed.  In
> an actual, not test program like this one, the data in the second
> form would be dependent on the first form.
>
> What did I do wrong?
>
> Thanks in advance.
>
> Here is the code:
>
> ================
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
>    "DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
>   <head>
>     <title>Project 4-4: Age Calculator</title>
>   </head>
>   <body>
>     <h2>Project 4-4: Age Calculator</h2>
> <?php
>     // if form not yet submitted
>     // display form
>
> $ender = 0;
> begin:
>
>         if($ender == 1)
>                 exit();
>     if (!isset($_POST['dob']))
>     {
> start1:
>
> echo "  <form method=\"post\" action=\"agecalc2.php\">";
> echo "  Enter your date of birth, in mm/dd/yyyy format: <br />";
> echo "  <input type=\"text\" name=\"dob\" />";
> echo "      <p>";
> echo "  <input type=\"submit\" name=\"submit\" value=\"Submit\" />";
> echo "  </form>";
>
> goto begin;
>     // if form submitted
>     // process form input
>     }
> else
>     {
> starter:
> if (isset($_POST['cat']))
>         goto purr;
>       // split date value into components
>       $dateArr = explode('/', $_POST['dob']);
>
>       // calculate timestamp corresponding to date value
>       $dateTs = strtotime($_POST['dob']);
>
>       // calculate timestamp corresponding to 'today'
>       $now = strtotime('today');
>
>       // check that the value entered is in the correct format
>       if (sizeof($dateArr) != 3) {
>         die('ERROR: Please enter a valid date of birth');
>       }
>
>       // check that the value entered is a valid date
>       if (!checkdate($dateArr[0], $dateArr[1], $dateArr[2])) {
>         die('ERROR: Please enter a valid date of birth');
>       }
>
>       // check that the date entered is earlier than 'today'
>       if ($dateTs >= $now) {
>         die('ERROR: Please enter a date of birth earlier than today');
>       }
>
>       // calculate difference between date of birth and today in days
>       // convert to years
>       // convert remaining days to months
>       // print output
>       $ageDays = floor(($now - $dateTs) / 86400);
>       $ageYears = floor($ageDays / 365);
>       $ageMonths = floor(($ageDays - ($ageYears * 365)) / 30);
>       echo "You are approximately $ageYears years and $ageMonths months
> old.";
>       goto meow;
>  }
>
> meow:
>         if (!isset($_POST['dob']))
>                 goto begin;
>     if (!isset($_POST['cat']))
>     {
>
>
> echo "  <form method=\"post\" action=\"agecalc2.php\">";
> echo "  <br /><br />Enter your kitten's name: <br />";
> echo "  <input type=\"text\" name=\"cat\" />";
> echo "  <p>";
> echo "      <input type=\"submit\" name=\"submit\" value=\"Submit
> Kitten\" />";
> echo "    </form>";
>
> }
> else
> {
> purr:
>         $name_cat = $_POST['cat'];
>
>         echo "Your Kitten is $name_cat";
>         $ender = 1;
> }
> if ($ender == 0)
>         goto begin;
> first_step:
> ?>
>
>   </body>
> </html>
>
> ============
>
> Ethan
>
> MySQL 5.1  PHP 5  Linux [Debian (sid)]
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>  _____
>
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 10.0.1170 / Virus Database: 1435/3323 - Release Date: 12/18/10
>
>

--- End Message ---
--- Begin Message ---
You approved all of us, no matter the  peer review, when you signed up
or the list. The accumulation of knowledge,
is insurmountable when delivered as a whole, but devourable if you
need satiation of appetite.

--- End Message ---
--- Begin Message ---
or maybe it's saturday morning and i'm drunk?

--- End Message ---
--- Begin Message ---
check out my new sig.

--- End Message ---
--- Begin Message ---
On 12/17/2010 12:52 PM, Sorin Buturugeanu wrote:
Hello all!

I have a question regarding arrays and the way I can use a value.

Let's say I have this string:

$s = 'banana,apple,mellon,grape,nut,orange'

I want to explode it, and get the third value. For this I would normally do:

$a = explode(',', $s);
echo $s[2];

That's all fine, but is there a way to get the value directly, without
having to write another line in my script. I mean something like this:

echo explode(',', $s)[2];

or

echo {explode(',', $s)}[2];

I couldn't find out this answer anywhere, that's why I posted here.

Cheers and thanks!


Sure it CAN be done.  Nobody laugh too loud here... But...

<?php

$s = 'banana,apple,mellon,grape,nut,orange';
echo preg_replace('/([^,]+,){3}([^,]+).*/', '$2', $s);

?>
Outputs: grape

The {3} part is equivalent to the array position. Change that number, and you change which word will get displayed.

Jim Lucas

--- End Message ---
--- Begin Message ---
Hi everyone,

I just started using PHPmailer for one project that requires SMTP authentication (if I said that right).. and all is well.. but I want to ask now before it might get outta hand later:

How many comma-delim'ed addresses can I stuff in $BCC_recipients before I have problems (need to restructure the design)?

------------------------------------------------------
require("php_inc/class.phpmailer.php");
$mail = new PHPMailer();
$BCC_recipients = "[email protected],[email protected]"; // <---just an example
$arrBCC_recipients = explode(",", $BCC_recipients);
foreach ($arrBCC_recipients as $email2stuffInBCC) {
        $mail->AddBcc($email2stuffInBCC);
}
if(!$mail->Send()) { // problem ....
------------------------------------------------------


For now there will be less than 100 Bcc recipients.. but later, more. I don't know if/when it will ever grow past 1,000.

------------
Govinda






--- End Message ---
--- Begin Message ---
Dear List -

Thanks to all for your EXCELLENT comments. I definitly agree that goto is a command to be avoided at all costs. In this case, I could not figure out how to acheive the desired result without the goto. So.... being a newbie, I humbly request that you show [and at the same time teach] me how to rewrite the code to eleiminate the goto.

Additionally, would you please do the same for the code I list below. This code runs perfectly.
==============
This is the form:

<html>
<head>
<title>Data Search</title>
<center><h4></b>Data Search</h3></center>
</head>
<body>

<form action="srchrhsptl2.php" method="post">

<center>Site: <input type="text" name="Site" value="AA" />
Record Number: <input type="text" name="MedRec"  />
First Name: <input type="text" name="Fname" />
Last Name: <input type="text" name="Lname" /><br /><br />
Phone: <input type="text" name="Phone" />
Height: <input type="decimal" name="Height" /></input><br /><br />
Male<input type="radio" name="Sex" value = "0"></input>
Female<input type="radio" name="Sex" value = "1"></input><br /><br /><br />
<input type="submit" /><br /><br />
<input type="reset" value = "Clear Form"  /></center>
</form>

</body>
</html>

*******************
This is the program -

<html><body>
<title>Search of Data</title>
<pre>
<?php

require '/var/www/pass.inc';

$db = "hospital2";

$cxn = mysqli_connect($host,$user,$password,$db);

$ste = $_POST['Site'];
$req = $_POST['MedRec'];
$fnm = $_POST['Fname'];
$lnm = $_POST['Lname'];
$phn = $_POST['Phone'];
$hgt = $_POST['Height'];
//$sex = $_REQUEST['Sex'];
//print_r($_POST);

$sitedone = 0;
$recdone = 0;
$fnmdone = 0;
$lnmdone = 0;
$phndone = 0;
$hgtdone = 0;
$sexdone = 0;

$sql1 =  "select * from  Intake3 where ";
if(isset($_POST['Site']) && trim($_POST['Site']) != '')
{
        $sql1 = $sql1 . "site = '$ste'";
        $sitedone = 1;
        goto end;
}


 if(isset($_POST['MedRec'])&& trim($_POST['MedRe']) != '')
{
        $sql1 = $sql1 . "MedRec = '$req'";
        $recdone = 1;
        goto end;
}

if(isset($_POST['Fname'])&& trim($_POST['Fname']) != '')
{
        $sql1 = $sql1 . "Fname = '$fnm";
        $fnmdone = 1;
        goto end;
}

if(isset($_POST['Lname'])&& trim($_POST['Lname']) != '')
{
        $sql1 = $sql1 . "Lname  = '$lnm'";
        $lnmdone = 1;
        goto end;
}

if(isset($_POST['Phone'])&& trim($_POST['Phone']) != '')
{
        $sql1 = $sql1 . "Phone = '$phn'";
        $phndone = 1;
}

if(isset($_Request['Sex'])&& trim($_POST['Sex']) != '' )
{
        if ($_REQUEST["Sex"] == "0")
        $sex = 'Male';
else
        $sex = 'Female';

        $sql1 = $sql1 . "  = '$sex'";
        $sexdone = 1;
}

if(isset($_POST['Hx'])&& trim($_POST['Hx']) != '')
{
        $sql1 = $sql1 . "Hx  = '$hx'";
        $done = 1;
}


end:

if ($sitedone == 1)
        goto recder;
if ($sitedone == 0)
{
if(isset($_POST['Site']) && trim($_POST['Site']) != '')
        {
                $sql1 = $sql1 . " &&  (Site = '$ste')";
                goto recder;
        }
}

recder:
if ($recdone == 1)
        goto fnmer;
if ($recdone == 0)
{
if(isset($_POST['MedRec'])&& trim($_POST['MedRec']) != '')
        {
                $sql1 = $sql1 . " &&  (MedRec = '$req')";
                $recdone = 1;
                goto fnmer;
        }
}


fnmer:
if($fnmdone == 1)
        goto lnmer;
if($fnmdone == 0)
{
if(isset($_POST['Fname'])&& trim($_POST['Fname']) != '')
        {
                $sql1 = $sql1 . " &&  (Fname = '$fnm')";
                $fnmdone = 1;
                goto lnmer;
        }
}

lnmer:
if($lnmdone == 1)
        goto phner;
if($lnmdone == 0)
{
if(isset($_POST['Lname'])&& trim($_POST['Lname']) != '')
        {
                $sql1 = $sql1 . " &&  (Lname = '$lnm')";
                $lnmdone = 1;
                goto phner;
        }
}

phner:
if($phndone == 1)
        goto hgter;
if($phndone == 0)
{
if(isset($_POST['Phone'])&& trim($_POST['Phone']) != '')
        {
                $sql1 = $sql1 . " &&  (Phone = '$phn')";
                $phndone = 1;
                goto hgter;
        }
}

hgter:
if($hgtdone == 1)
        goto sexer;
if($hgtdone == 0)
{
if(isset($_POST['Height'])&& trim($_POST['Height']) != '')
        {
                $sql1 = $sql1 . " &&  (Height = '$hgt')";
                $hgtdone = 1;
                goto sexer;
        }
}

sexer:
if($sexdone == 1)
        goto ender;
if($sexdone == 0)
{
if(isset($_REQUEST['Sex'])&& trim($_REQUEST['Sex']) != '')
        {
                $sql1 = $sql1 . " &&  (sex = '$sex')";
                $done = 1;
                goto ender;
        }
}



ender:

$sql1 = $sql1 . ";";
printf("<br />");
//printf($sql1);
$result = mysqli_query($cxn, $sql1);

if(($num = mysqli_num_rows($result)) == 0)
        die ("No Records Retrieved");
?>
<center><b>Search Results</b></center><br />

<center><table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
<tr class=\"heading\">
<th>Site</th>
<th>Medical Record</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</td>
<th>Height</td>
<th>Sex</td>
<th>History</td>
</tr>

<?php
// printf("<br />To exit, click the EXIT button below.<br /><br />"); // printf("%s\t%s\t%s\t%s\t%s<br />","Site","Record", "Weight", "Height", "BMI");
        while($row = mysqli_fetch_array($result))

        {
        echo "<tr>\n";

        echo "<td> $row[0] </td>\n";
        echo "<td> $row[1] </td>\n";
        echo "<td> $row[3] </td>\n";
        echo "<td> $row[4] </td>\n";
        echo "<td> $row[2] </td>\n";
        echo "<td> $row[5] </td>\n";
        echo "<td> $row[6] </td>\n";
        echo "<td> $row[7] </td>\n";
        echo "</tr>\n";

/*              $site2 = $row[0];
                $record2 = $row[1];
                $bmi2 = $row[2];
                $wgt2 = $row[3];
                $hgt2 = $row[4];
printf("%s\t%d\t%d\t%d\t%3.1f<br />", $site2, $record2, $wgt2, $hgt2, $bmi2);*/
        }


/*printf("%s\t%s\t%s\t%s\t%s<br />","Site","Record", "BMI", "Weight", "Height");
printf("<br />");
while(  $row = mysqli_fetch_array($result))
{
                $site2 = $row[0];
                $record2 = $row[1];
                $bmi2 = $row[2];
                $wgt2 = $row[3];
                $hgt2 = $row[4];
printf("%s\t%d\t%3.1f\t%d\t%d<br />", $site2, $record2, $bmi2, $wgt2, $hgt2);

}*/
mysqli_free_result($result);
mysqli_close($cxn);

?>

</pre></body></html>

==============
THANK YOU EVER SO MUCH FOR YOUR HELP.

Ethan


--- End Message ---
--- Begin Message ---
On 10-12-19 12:17 AM, Ethan Rosenberg wrote:
Dear List -

Thanks to all for your EXCELLENT comments.  I definitly agree that
goto is a command to be avoided at all costs.

Closed-minded drivel (or you're buttering up the popular opinion crowd). A better approach is that goto should be used with caution.

As for doing your homework for you... ummm no thanks. You should take the time to do the exercise so you gain the benefit of experience.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--- End Message ---
--- Begin Message ---
On Sat, Dec 18, 2010 at 11:44 PM, Robert Cummings <[email protected]> wrote:
> On 10-12-19 12:17 AM, Ethan Rosenberg wrote:
>>
>> Dear List -
>>
>> Thanks to all for your EXCELLENT comments.  I definitly agree that
>> goto is a command to be avoided at all costs.
>
> Closed-minded drivel (or you're buttering up the popular opinion crowd). A
> better approach is that goto should be used with caution.
>
> As for doing your homework for you... ummm no thanks. You should take the
> time to do the exercise so you gain the benefit of experience.

I would have thought school would have been out on account of Christmas and all.

In any event, here's my rewrite:

switch (true)
{
        case isset($_POST['Site']) && trim($_POST['Site']) != '':
                $sql1 = $sql1 . "site = '$ste'";
                break;
        case isset($_POST['MedRec'])&& trim($_POST['MedRe']) != '':
                $sql1 = $sql1 . "MedRec = '$req'";
                break;
        // ...
        default:
                if(isset($_Request['Sex'])&& trim($_POST['Sex']) != '' )
                {
                        if ($_REQUEST["Sex"] == "0")
                                $sex = 'Male';
                        else
                                $sex = 'Female';

                        $sql1 = $sql1 . "  = '$sex'";
                        $sexdone = 1;
                }

        if(isset($_POST['Hx'])&& trim($_POST['Hx']) != '')
        {
                $sql1 = $sql1 . "Hx  = '$hx'";
                $done = 1;
        }
}

You could also do an if / else if / else if / ... / else.

--- End Message ---

Reply via email to