php-general Digest 24 Jan 2011 19:17:11 -0000 Issue 7150

Topics (messages 310994 through 311007):

Re: No SMTP server? Can't get mail()
        310994 by: Paul S
        310995 by: David Robley

Re: Zend memory manager
        310996 by: Tommy Pham
        311007 by: Adi Mutu

Mysql search query ignoring dots
        310997 by: Barbara Picci
        310998 by: Tommy Pham

Re: Different sessions, same client
        310999 by: Steve Staples
        311000 by: Tommy Pham
        311001 by: Donovan Brooke
        311003 by: Tommy Pham

preg_replace question
        311002 by: Merlin Morgenstern
        311004 by: David Harkness
        311005 by: Alex Nikitin
        311006 by: Jim Lucas

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 --- On Sun, 23 Jan 2011 12:40:25 +0700, David Robley <[email protected]> wrote:

Paul S wrote:
I'm a little new to PHP.

David starts:
Nobody seems to have mentioned it, but the SMTP info is only needed on a Win
system. The following is an extract from a default php.ini file:


Well, I have just RTF mail() Manual and you are ABSOLUTELY correct SMTP info is not needed for PHP mail(). SORRY for that confusion.

Where my confusion started, I guess, and why I was trying to get confirmation of the SMTP server name and try mail() at the same time was the mention of both in a "SMTP" option in the default php config.php file for the email list program that I am trying to get running, as follows ...

<begin quote># If you want to use the PHPMailer class from phpmailer.sourceforge.net, set the following
# to 1. If you tend to send out html emails, it is recommended to do so.
define("PHPMAILER",1);

# To use a SMTP please give your server hostname here, leave it blank to use the standard
# PHP mail() command.
define("PHPMAILERHOST",'');"<end quote>

I'm still confused! :-( I can't even put my confusion into words hahahaha.

Is it SMTP server that uses sendmail or sendmail uses an SMTP server???

PHPMAILER doesn't need an SMTP server name?

OK, I've just googled PHPMAILERHOST and see "confusing" a few times. I will follow it from here for now.

mail() should be functional; if mail() is returning true then your next step
in the debug process would be to check the mail logs, if they are
accessible to you.

Well, I am glad you agree!

no access that I know of.
No telnet.
and
no helpful replies from the staff, ever
and
No PHPINFO and no echo of even what Apache server version I am using (see blocked info in my post above) RANT: I could put just this whole project aside by just using an old v. simple emaillist program that uses mail().! I'm about to give up here.


On the other hand, using an SMTP class will give you a bit more
functionality.


I'll check SMTP classes out. Thank you.


--
Using Opera's revolutionary email client: http://www.opera.com/mail/

--- End Message ---
--- Begin Message ---
Paul S wrote:

> On Sun, 23 Jan 2011 12:40:25 +0700, David Robley <[email protected]>
> wrote:
> 
>> Paul S wrote:
>>> I'm a little new to PHP.
> 
> David starts:
>> Nobody seems to have mentioned it, but the SMTP info is only needed on a
>> Win
>> system. The following is an extract from a default php.ini file:
>>
> 
> Well, I have just RTF mail() Manual and you are ABSOLUTELY correct SMTP
> info is not needed for PHP mail(). SORRY for that confusion.
> 
> Where my confusion started, I guess, and why I was trying to get
> confirmation of the SMTP server name
> and try mail() at the same time was the mention of both in a "SMTP" option
> in the default php config.php
> file for the email list program that I am trying to get running, as
> follows ...
> 
> <begin quote># If you want to use the PHPMailer class from
> phpmailer.sourceforge.net, set the following
> # to 1. If you tend to send out html emails, it is recommended to do so.
> define("PHPMAILER",1);
> 
> # To use a SMTP please give your server hostname here, leave it blank to
> use the standard
> # PHP mail() command.
> define("PHPMAILERHOST",'');"<end quote>

This gives you the option to use mail() (not SMTP) on the localhost - i.e
the server where the script is running, probably your ISP or hosting
company;

or;

to set the MAILERHOST to an external mail server using SMTP, maybe gmail,
yahoo mail, the mail provided by your ISP


> I'm still confused! :-( I can't even put my confusion into words hahahaha.
> 
> Is it SMTP server that uses sendmail or sendmail uses an SMTP server???

Not going there if you are as fresh to the Internet as your question
suggests :-)
> 
> PHPMAILER doesn't need an SMTP server name?
> 
> OK, I've just googled PHPMAILERHOST and see "confusing" a few times. I
> will follow it from here for now.
> 
>> mail() should be functional; if mail() is returning true then your next
>> step
>> in the debug process would be to check the mail logs, if they are
>> accessible to you.
> 
> Well, I am glad you agree!
> 
> no access that I know of.
> No telnet.
> and
> no helpful replies from the staff, ever
> and
> No PHPINFO and no echo of even what Apache server version I am using (see
> blocked info in my post above)
> RANT: I could put just this whole project aside by just using an old v.
> simple emaillist program that uses mail().! I'm about to give up here.
> 
>>
>> On the other hand, using an SMTP class will give you a bit more
>> functionality.
>>
> 
> I'll check SMTP classes out. Thank you.

phpMailer is an SMTP class :-) See also swiftmail, and probably quite a few
others. Generally they will handle both sending mail from the localhost -
where the script is, mail() - or a remote SMTP host.




Cheers
-- 
David Robley

Happiness is finding special characters .
Today is Prickle-Prickle, the 24th day of Chaos in the YOLD 3177. 


--- End Message ---
--- Begin Message ---
>From the search results, you'd see several links http://forums.zend.com.
I'd say that's the best place to ask ;)

 

From: Adi Mutu [mailto:[email protected]] 
Sent: Sunday, January 23, 2011 11:07 PM
To: Tommy Pham
Cc: [email protected]; Daniel Brown
Subject: Re: [PHP] Zend memory manager

 

Of course I have tried, but nothing .......

The results or only aboyt  emalloc/pemalloc familly.

Nothing lower level about how these 2 functions are implemented or about the
mm_heap struct of mm_block.....

 

 

 

 

 


--- End Message ---
--- Begin Message ---
I have asked also there......but no answer....But honestly i don't understand 
why you have reccomended me that forum......because i thougt these mailing 
lists are about php developing.......
Thanks,A.


      

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

I have to perform a mysql query in a table with millions of records.
I've full-text indexed my search field and I'm searching with MATCH AGAINST.
But there is a problem. In this field there are company names that contain dots, for istance I've "PO.SE. srl" and I want to find it if the user search for: "POSE" or "PO.SE" or "P.O.S.E." etc. I googled in the web but I don't find any solution. I don't want to add a new field with the cleaned version of my string because I would like to solve with the query and I prefer that the mysql table not become too big. But if I will not find a different solution, I will use this escamotage. I've find a post that is similar but the solution don't seem to solve my situation.
You can see it at the url:
http://forums.mysql.com/read.php?10,395557,395584#msg-395584
In my case replace(email, '.', '') = replace(theSearchValue, '.', '');
is indifferent and don't change my results.

My query, searching "POSE", is:

select aziende.* from aziende where 1>0 AND (MATCH(aziende.ragione_sociale) AGAINST('+POSE' IN BOOLEAN MODE) OR (replace(aziende.ragione_sociale, '.', '') = replace('POSE', '.', '')) order by aziende.ragione_sociale limit 0, 10

The alternative choice could be REGEXP but I've red that it make my query slow in a table of millions of records and I don't know how to exclude dots in the regular expression.

Can anyone help me?

Thanks in advance.
Barbara

--
------------------------
Barbara Picci
Micro srl
viale Marconi 222, 09131 Cagliari  - tel. (+39) 070400240
http://www.microsrl.com

--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Barbara Picci [mailto:[email protected]]
> Sent: Monday, January 24, 2011 4:51 AM
> To: [email protected]
> Subject: [PHP] Mysql search query ignoring dots
> 
> Hi all,
> 
> I have to perform a mysql query in a table with millions of records.
> I've full-text indexed my search field and I'm searching with MATCH
> AGAINST.
> But there is a problem. In this field there are company names that contain
> dots, for istance I've "PO.SE. srl" and I want to find it if the user
search for:
> "POSE" or "PO.SE" or "P.O.S.E." etc.
> I googled in the web but I don't find any solution. I don't want to add a
new
> field with the cleaned version of my string because I would like to solve
> with the query and I prefer that the mysql table not become too big. But
if I
> will not find a different solution, I will use this escamotage.
> I've find a post that is similar but the solution don't seem to solve my
> situation.
> You can see it at the url:
> http://forums.mysql.com/read.php?10,395557,395584#msg-395584
> In my case replace(email, '.', '') = replace(theSearchValue, '.', ''); is
> indifferent and don't change my results.
> 
> My query, searching "POSE", is:
> 
> select aziende.* from aziende where 1>0 AND
> (MATCH(aziende.ragione_sociale) AGAINST('+POSE' IN BOOLEAN MODE) OR
> (replace(aziende.ragione_sociale, '.', '') = replace('POSE', '.',
> '')) order by aziende.ragione_sociale limit 0, 10
> 
> The alternative choice could be REGEXP but I've red that it make my query
> slow in a table of millions of records and I don't know how to exclude
dots
> in the regular expression.
> 
> Can anyone help me?
> 
> Thanks in advance.
> Barbara
> 
> --
> ------------------------
> Barbara Picci
> Micro srl
> viale Marconi 222, 09131 Cagliari  - tel. (+39) 070400240
> http://www.microsrl.com
> 

I don't see anything relevant regarding PHP.  As for ' a table of millions
of records,' that sounds like questions for the DBA.

Regards,
Tommy


--- End Message ---
--- Begin Message ---
On Sun, 2011-01-23 at 17:40 -0800, Tommy Pham wrote:
> > -----Original Message-----
> > From: Tommy Pham [mailto:[email protected]]
> > Sent: Sunday, January 23, 2011 5:23 PM
> > To: 'Paul M Foster'
> > Cc: '[email protected]'; 'Thijs Lensselink'
> > Subject: RE: [PHP] Different sessions, same client
> > 
> > > -----Original Message-----
> > > From: Thijs Lensselink [mailto:[email protected]]
> > > Sent: Sunday, January 23, 2011 12:21 AM
> > > To: [email protected]
> > > Subject: Re: [PHP] Different sessions, same client
> > >
> > > -----BEGIN PGP SIGNED MESSAGE-----
> > > Hash: SHA1
> > >
> > > On 01/23/2011 07:33 AM, Paul M Foster wrote:
> > > > Storing any sort of login/auth data in cookies has regularly been
> > > > panned on this list. The preference seems to be to store whatever
> > > > login/auth information *must* be stored in the $_SESSION variable.
> > > >
> > > > Well and good. My problem, however, is that I have multiple
> > > > applications in different tabs running on the same server, which may
> > > > all use the same sub-variables, like "username". As a result, they
> > > > run into
> > > each other.
> > > > One application will think I'm logged in when I'm not logged in to
> > > > that application, but to another in the same browser on the same box.
> > > >
> > > > So my question is how to prevent this using the standard PHP
> > > > functions relating to sessions. I'd like different applications in
> > > > different tabs on the same box/browser to have different sessions,
> > > > so they don't share data.
> > > >
> > > > Thoughts?
> > > >
> > > > Paul
> > > >
> > >
> > >
> > > Using session_name will allow you to run two different sessions in the
> > > same browser.
> > >
> > > session_name('app1');
> > > session_start();
> > 
> > Paul,
> > 
> > I'd would go with session_name($_SERVER['SCRIPT_NAME']) or
> > session_name(substr($_SERVER['SCRIPT_NAME'], 0,
> > strripos($_SERVER['SCRIPT_NAME'], '/')).  My regex skills sucks so I can't 
> > give
> > you a sample using regex.  But you get the idea.
> > 
> > It's easier to get a particular app's relevant data to the URL while not 
> > hard
> > coding the session name, eventually giving your app(s) more flexibility
> > especially if you may have multiple URLs mapped to an app serving
> > different purposes/clients.
> > 
> > Regards,
> > Tommy
> 
> Forgot to mention that this assumes your app's design is MVC like with a 
> single point entry only.
> 
> 

Hey guys...

I too once tried this, basically so that I could stop users logging in
on multiple tabs, and if they did, then it would kill the previous login
(or not allow them to be logged in as they would be logged in still).  I
had so many issues, that I abandoned it.

After reading this thread, I thought I would try Tommy's suggestion
about using a unique named session... so I just tried this:

<?php
session_name(uniqid());
session_start();
echo session_id();
?>

YAY!  it worked!!

so then i tried this:
<?php
session_name(uniqid());
session_start();
$_SESSION['t_'. time()] = time();
echo session_id();
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>

and it doesn't preserve the older session information... so I must be
doing something wrong.  I can assume that because the name is being
regenerated new each time, that the old "previous" session is destroyed
(which would make sense) but then how can *I* ensure that each session
is going to be unique enough, but preserve "old" session information
too?  

I know it has to be possible, as my bank doesn't allow multiple tabs
while online banking.

/sigh  the joys of protecting users from themselves... 


--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Steve Staples [mailto:[email protected]]
> Sent: Monday, January 24, 2011 6:31 AM
> To: Tommy Pham
> Cc: 'Paul M Foster'; [email protected]
> Subject: RE: [PHP] Different sessions, same client
> 
> On Sun, 2011-01-23 at 17:40 -0800, Tommy Pham wrote:
> > > -----Original Message-----
> > > From: Tommy Pham [mailto:[email protected]]
> > > Sent: Sunday, January 23, 2011 5:23 PM
> > > To: 'Paul M Foster'
> > > Cc: '[email protected]'; 'Thijs Lensselink'
> > > Subject: RE: [PHP] Different sessions, same client
> > >
> > > > -----Original Message-----
> > > > From: Thijs Lensselink [mailto:[email protected]]
> > > > Sent: Sunday, January 23, 2011 12:21 AM
> > > > To: [email protected]
> > > > Subject: Re: [PHP] Different sessions, same client
> > > >
> > > > -----BEGIN PGP SIGNED MESSAGE-----
> > > > Hash: SHA1
> > > >
> > > > On 01/23/2011 07:33 AM, Paul M Foster wrote:
> > > > > Storing any sort of login/auth data in cookies has regularly
> > > > > been panned on this list. The preference seems to be to store
> > > > > whatever login/auth information *must* be stored in the $_SESSION
> variable.
> > > > >
> > > > > Well and good. My problem, however, is that I have multiple
> > > > > applications in different tabs running on the same server, which
> > > > > may all use the same sub-variables, like "username". As a
> > > > > result, they run into
> > > > each other.
> > > > > One application will think I'm logged in when I'm not logged in
> > > > > to that application, but to another in the same browser on the same
> box.
> > > > >
> > > > > So my question is how to prevent this using the standard PHP
> > > > > functions relating to sessions. I'd like different applications
> > > > > in different tabs on the same box/browser to have different
> > > > > sessions, so they don't share data.
> > > > >
> > > > > Thoughts?
> > > > >
> > > > > Paul
> > > > >
> > > >
> > > >
> > > > Using session_name will allow you to run two different sessions in
> > > > the same browser.
> > > >
> > > > session_name('app1');
> > > > session_start();
> > >
> > > Paul,
> > >
> > > I'd would go with session_name($_SERVER['SCRIPT_NAME']) or
> > > session_name(substr($_SERVER['SCRIPT_NAME'], 0,
> > > strripos($_SERVER['SCRIPT_NAME'], '/')).  My regex skills sucks so I
> > > can't give you a sample using regex.  But you get the idea.
> > >
> > > It's easier to get a particular app's relevant data to the URL while
> > > not hard coding the session name, eventually giving your app(s) more
> > > flexibility especially if you may have multiple URLs mapped to an
> > > app serving different purposes/clients.
> > >
> > > Regards,
> > > Tommy
> >
> > Forgot to mention that this assumes your app's design is MVC like with a
> single point entry only.
> >
> >
> 
> Hey guys...
> 
> I too once tried this, basically so that I could stop users logging in on
> multiple tabs, and if they did, then it would kill the previous login (or not
> allow them to be logged in as they would be logged in still).  I had so many
> issues, that I abandoned it.
> 
> After reading this thread, I thought I would try Tommy's suggestion about
> using a unique named session... so I just tried this:
> 
> <?php
> session_name(uniqid());
> session_start();
> echo session_id();
> ?>
> 
> YAY!  it worked!!
> 
> so then i tried this:
> <?php
> session_name(uniqid());
> session_start();
> $_SESSION['t_'. time()] = time();
> echo session_id();
> echo '<pre>';
> print_r($_SESSION);
> echo '</pre>';
> ?>
> 
> and it doesn't preserve the older session information... so I must be doing
> something wrong.  I can assume that because the name is being
> regenerated new each time, that the old "previous" session is destroyed
> (which would make sense) but then how can *I* ensure that each session is
> going to be unique enough, but preserve "old" session information too?
> 
> I know it has to be possible, as my bank doesn't allow multiple tabs while
> online banking.
> 
> /sigh  the joys of protecting users from themselves...

Steve,

The problem with uniqid() is it's based on unix timestamp, IIRC.  So the 
session name always changes and it's impossible to figure out what's the 
previous session name is so how could you get the previously saved session 
data.  That's why I suggested the session name based on URL/URI.  If the app 
does not have a MVC like desgin, you could use a particular parameter's value 
to ensure that session name is valid for certain purpose.  For example:

http://server/training/schedules/ is different from 
http://server/events/schedules/.  

Should the person in charge of both needs to maintain them, it still could be 
done getting the session_name based on the url requesting.  Thus, you'd be able 
to get the previously saved session data.  Also, if the need arises, you could 
also get data from different sessions if the business needs requires it because 
you know exactly what the session name based URL is.  As in the example above, 
the maintainer could check to ensure that the certain scheduled items wouldn't 
conflict each other in cases where the attendees are required to attend both :)

Regards,
Tommy





--- End Message ---
--- Begin Message ---
[snip]
<?php
session_name(uniqid());
session_start();
echo session_id();
?>

YAY!  it worked!!

so then i tried this:
<?php
session_name(uniqid());
session_start();
$_SESSION['t_'. time()] = time();
echo session_id();
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>

and it doesn't preserve the older session information... so I must be
doing something wrong.  I can assume that because the name is being
regenerated new each time, that the old "previous" session is destroyed
(which would make sense) but then how can *I* ensure that each session
is going to be unique enough, but preserve "old" session information
too?

I know it has to be possible, as my bank doesn't allow multiple tabs
while online banking.

/sigh  the joys of protecting users from themselves...


Hello,
What seems to be missing from this thread is talk about the root of the problem. You would never want to create the same cookie name for alike web-apps for the very reason Paul has discovered. Session_name works because it changes the name of the PHP session cookie. This is important
for CMS builders, Forum builders, or other app builders etc..

Paul mentions:
"Storing any sort of login/auth data in cookies has regularly been panned on this list. The preference seems to be to store whatever login/auth information *must* be stored in the $_SESSION variable."

Well, there are only 2 ways that I know of to retain *state* in a web
app (no matter what web server-side language you are working with), which are cookies or passing a variable in all links... so I would re-phrase Paul's statement above to say, to retain state, there is *always* some reference to login data (whether direct or indirect (encrypted)), but right, it's not a good idea to store AUTH info.

Extending Tedd's suggestion, Instead of a unique ID for a session name (most often session *cookie*), I really prefer a hash of something that results in a recognizable cookie name over something random. In my opinion only, it is a bit shady to create a cookie that is unrecognizable. At the least, when I am managing my own cookies, I will delete "wierd" cookie names. What I usually do for sessions is
create a cookie name that is based on the domain, and also lists the
word "session"... so a format something like:

<domain>_session

A format such as above lets the user know right away where the cookie
comes from and what it does. I would post code, but I haven't written the hash in PHP yet.

One last note about this hash, I always include a default to the IP address in the case of development, or if the site does not have a
domain name.

Oh, and one last last note, Accessing a web app with localhost does
not work well with cookies either.. so in my hash, I redirect those
who access my app from "localhost" to the localhost IP (127.0.0.1)
right away.

I know that last part is a bit complicated, but I help write the hash
if the list is interested. I was really surprised to find no mention of
this on the PHP's examples of sessions.

Sorry for the long post!

Donovan



--
D Brooke

--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Donovan Brooke [mailto:[email protected]]
> Sent: Monday, January 24, 2011 7:49 AM
> Cc: [email protected]
> Subject: Re: [PHP] Different sessions, same client
> 
> [snip]
> > <?php
> > session_name(uniqid());
> > session_start();
> > echo session_id();
> > ?>
> >
> > YAY!  it worked!!
> >
> > so then i tried this:
> > <?php
> > session_name(uniqid());
> > session_start();
> > $_SESSION['t_'. time()] = time();
> > echo session_id();
> > echo '<pre>';
> > print_r($_SESSION);
> > echo '</pre>';
> > ?>
> >
> > and it doesn't preserve the older session information... so I must be
> > doing something wrong.  I can assume that because the name is being
> > regenerated new each time, that the old "previous" session is
> > destroyed (which would make sense) but then how can *I* ensure that
> > each session is going to be unique enough, but preserve "old" session
> > information too?
> >
> > I know it has to be possible, as my bank doesn't allow multiple tabs
> > while online banking.
> >
> > /sigh  the joys of protecting users from themselves...
> 
> 
> Hello,
> What seems to be missing from this thread is talk about the root of the
> problem. You would never want to create the same cookie name for alike
> web-apps for the very reason Paul has discovered. Session_name works
> because it changes the name of the PHP session cookie. This is important for
> CMS builders, Forum builders, or other app builders etc..
> 

That's exactly part of the 1st point of 3 points I've described in another 
thread.  We don't exactly know the current/future requirements of the business, 
the different apps being used, who developed those apps, in what way are each 
app's data need be interconnected/shared, etc.  So we can only give suggestions 
based on limited info disclosure for whatever the reason maybe, perhaps 
security or something else.

Regards,
Tommy

> Paul mentions:
> "Storing any sort of login/auth data in cookies has regularly been panned on
> this list. The preference seems to be to store whatever login/auth
> information *must* be stored in the $_SESSION variable."
> 
> Well, there are only 2 ways that I know of to retain *state* in a web app (no
> matter what web server-side language you are working with), which are
> cookies or passing a variable in all links... so I would re-phrase Paul's
> statement above to say, to retain state, there is
> *always* some reference to login data (whether direct or indirect
> (encrypted)), but right, it's not a good idea to store AUTH info.
> 
> Extending Tedd's suggestion, Instead of a unique ID for a session name
> (most often session *cookie*), I really prefer a hash of something that
> results in a recognizable cookie name over something random. In my
> opinion only, it is a bit shady to create a cookie that is unrecognizable. At
> the least, when I am managing my own cookies, I will delete "wierd" cookie
> names. What I usually do for sessions is create a cookie name that is based
> on the domain, and also lists the word "session"... so a format something
> like:
> 
> <domain>_session
> 
> A format such as above lets the user know right away where the cookie
> comes from and what it does. I would post code, but I haven't written the
> hash in PHP yet.
> 
> One last note about this hash, I always include a default to the IP address in
> the case of development, or if the site does not have a domain name.
> 
> Oh, and one last last note, Accessing a web app with localhost does not
> work well with cookies either.. so in my hash, I redirect those who access
> my app from "localhost" to the localhost IP (127.0.0.1) right away.
> 
> I know that last part is a bit complicated, but I help write the hash if the 
> list
> is interested. I was really surprised to find no mention of this on the PHP's
> examples of sessions.
> 
> Sorry for the long post!
> 
> Donovan
> 
> 
> 
> --
> D Brooke
> 


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

I am trying to replace certain words inside a text with php. Unfortunatelly my function is creating invalid html as output.

For example the words "beagle" and "welpen" have to be replaced inside this text: "süße knuffige Beagle Welpen ab sofort"

My result looks like this:
zwei süße knuffige <a href="/bsp/hunde,beagle">Beagle <a href="/bsp/hunde">Welpen</a></a>

The problem is, that my function is not closing the href tag before it starts to replace the next item.

Here is the code: ################################################################################################
        // create internal links
        function 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);
        }
        
################################################################################################I

I could not find a way to fix this and I would be happy for some help. Thank you in advance!

Merlin

--- End Message ---
--- Begin Message ---
Without seeing the code that creates the arrays, it's tough to see the
problem. It looks like the first replacement is catching "Beagle Welpen"
entirely since the closing </a> tag gets placed after "Welpen". Then the
second replacement does just "Welpen".

Also, you should have quotes around "link" when building the $replace[]
entry since the array access is outside quotes. Finally, you don't need $k
here at all.

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

David

--- End Message ---
--- Begin Message ---
If you declare your arrays, and set k to 0 first, put quotes around array
values and use the correct limit (you can default to -1), you will get
results, here is code and example (hopefully this helps you)


<?php
       function internal_links($str, $links, $limit=-1) {
               $pattern=array();
               $replace=array();
               $k=0;
               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);
       }

echo internal_links("süße knuffige Beagle Welpen ab sofort",
array(array('phrase'=>"beagle",
'link'=>"http://google.com";),array('phrase'=>"welpen",
'link'=>"http://wolframalpha.com";)), -1);

Output:
süße knuffige <a href="http://google.com";>Beagle</a> <a href="
http://wolframalpha.com";>Welpen</a> ab

~Alex

--- End Message ---
--- Begin Message ---
On 1/24/2011 8:00 AM, Merlin Morgenstern wrote:
> Hi there,
> 
> I am trying to replace certain words inside a text with php. Unfortunatelly my
> function is creating invalid html as output.
> 
> For example the words "beagle" and "welpen" have to be replaced inside this
> text: "süße knuffige Beagle Welpen ab sofort"
> 
> My result looks like this:
> zwei süße knuffige <a href="/bsp/hunde,beagle">Beagle <a
> href="/bsp/hunde">Welpen</a></a>
> 
> The problem is, that my function is not closing the href tag before it starts 
> to
> replace the next item.
> 
> Here is the code:
> ################################################################################################
> 
>     // create internal links
>     function 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);
>     }
>     
> ################################################################################################I
> 
> 
> I could not find a way to fix this and I would be happy for some help. Thank 
> you
> in advance!
> 
> Merlin
> 

Do you have control over the building of the initial "phrase" => "link" assoc?

If so, reverse the order of these two items.

Jim Lucas

--- End Message ---

Reply via email to