Go Back   WarriorForum - Internet Marketing Forums > Warrior Support Forums > Programming Talk
Register Blogs FAQ Social Groups CalendarHelp Desk

Reply
 
LinkBack Thread Tools
Old 11-23-2008, 11:36 AM   #1
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default I need some php code

Hopefully this might just be a few lines of code
and if so I would be grateful if someone could supply.
(My head is spinning with ereg pattern matching)


The requirement:

--------------------------------------------------------
I have a string which can contain any number of 'tokens'.
A token is text enclosed by {}

So for example

==> Hello {abc}, goodbye {def}

I want to remove the {} and replace each token
with a string derived from the token.

So I end up with

==> Hello xxx, goodbye yyy

where xxx = function (abc)
yyy = function (def)
--------------------------------------------------------

Thanks
Harvey

Harvey Segal is offline   Reply With Quote
Old 11-23-2008, 07:40 PM   #2
Enlightened Warrior
War Room Member
 
jaggyjay's Avatar
 
Join Date: Nov 2007
Location: New York, USA.
Posts: 536
Thanks: 12
Thanked 95 Times in 70 Posts
Default Re: I need some php code

Hey Harvey,

My php skills are still towards the "newbie" side.

But... you can post this prob on tek-tips.com. It's my
secret weapon whenever I have a coding question.
You should get an answer within a day or so.

Hope this helps.

Best,
-Jay

jaggyjay is offline   Reply With Quote
Old 11-23-2008, 07:45 PM   #3
Senior Warrior Member
War Room Member
 
Neil Morgan's Avatar
 
Join Date: Jun 2007
Location: Lanarkshire UK
Posts: 2,487
Thanks: 98
Thanked 314 Times in 223 Posts
Social Networking View Member's Twitter Profile 
Default Re: I need some php code

Hi Harvey

I did put some thought into this but it's not the easiest thing to do. I guess that's why you've not had a working reply yet.

The parsing of the original string to pull out all the tokens between the { and } symbols is the tricky bit because there's no one function that will do it.

It would need a function written to look for each { then read to the } to build the token, process and replace it (the easy part) and continue until all tokens had been read.

It's not that difficult, just a bit of a pain.

But I guess you already knew all that

Cheers,

Neil

Neil Morgan is offline   Reply With Quote
Old 11-23-2008, 08:27 PM   #4
DLGuard.com
War Room Member
 
samstephens's Avatar
 
Join Date: Nov 2004
Location: Central Coast, Australia
Posts: 6,199
Thanks: 67
Thanked 257 Times in 117 Posts
Social Networking View Member's Twitter Profile 
Default Re: I need some php code

I'd just do it using the string replace function:

$rawString = "Hello {abc}, goodbye {def}";

$newString = str_replace("{abc}", "Harvey", $rawString);
$newString = str_replace("{def}", "Sam", $newString);

echo $newString;


....gives you:

Hello Harvey, goodbye Sam


Quick and easy, just the way I like my code

cheers
Sam

DLGuard v4.6 Now Released!
Full integration with Amazon S3 for authenticated large file and high bandwidth downloads.

www.dlguard.com
Where size doesn't matter!
samstephens is offline   Reply With Quote
Old 11-24-2008, 02:43 AM   #5
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by jaggyjay View Post
But... you can post this prob on tek-tips.com.
Thanks Jay. I'll try them if I don't get an answer here.

Quote:
Originally Posted by samstephens View Post
= str_replace("{abc}", "Harvey", );
= str_replace("{def}", "Sam", );
Sam,

The problem is that abc, def etc are not known in advance

Quote:
Originally Posted by Neil Morgan View Post
The parsing of the original string to pull out all the tokens between the { and } symbols is the tricky bit because there's no one function that will do it.
Neil

Would it help to do it this way

1. Read through the string, extracting tokens into an array (along with
the corresponding replacement value). This will also give you the
number of tokens (n).

2. Loop n times through the string replacing each token in turn

Harvey

Harvey Segal is offline   Reply With Quote
Old 11-24-2008, 04:36 AM   #6
Banned
 
Join Date: Sep 2008
Posts: 91
Blog Entries: 1
Thanks: 0
Thanked 5 Times in 5 Posts
Contact Info
Send a message via Skype™ to ofir
Default Re: I need some php code

Learn regular expressions.

And then you will be able to replace patterns , such as "{XXXXXX}".
ofir is offline   Reply With Quote
Old 11-24-2008, 04:44 AM   #7
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by ofir View Post
Learn regular expressions.

And then you will be able to replace patterns , such as "{XXXXXX}".
That's actually the easy part of the problem.

Harvey Segal is offline   Reply With Quote
Old 11-24-2008, 10:54 AM   #8
Banned
War Room Member
 
Join Date: Mar 2008
Location: Long Beach, California
Posts: 112
Thanks: 29
Thanked 7 Times in 7 Posts
Social Networking View Member's Myspace Profile  View Member's FaceBook Profile  View Member's Twitter Profile 
Contact Info
Send a message via AIM to drewjones Send a message via Yahoo to drewjones Send a message via Skype™ to drewjones
Default Re: I need some php code

Is this for a membership site login and logout???

What I do to learn these kinda things is by studying code from the CMS programs (wordpress,joomla, etc.)

Then I implement it.

Hope this helps...
drewjones is offline   Reply With Quote
Old 11-24-2008, 11:00 AM   #9
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by drewjones View Post
Is this for a membership site login and logout???
No it's not


Quote:
Originally Posted by drewjones View Post
What I do to learn these kinda things is by studying code from the CMS programs (wordpress,joomla, etc.)

Then I implement it.
Well I could spend hours searching and studying OR ask an expert
at the Warrior forum !

Harvey

Harvey Segal is offline   Reply With Quote
Old 11-24-2008, 11:04 AM   #10
Banned
War Room Member
 
Join Date: Mar 2008
Location: Long Beach, California
Posts: 112
Thanks: 29
Thanked 7 Times in 7 Posts
Social Networking View Member's Myspace Profile  View Member's FaceBook Profile  View Member's Twitter Profile 
Contact Info
Send a message via AIM to drewjones Send a message via Yahoo to drewjones Send a message via Skype™ to drewjones
Default Re: I need some php code

Then what is the primary purpose...???

I spend the hours it takes to learn the simplest things....

I don't rely on others information...
drewjones is offline   Reply With Quote
Old 11-24-2008, 11:12 AM   #11
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by drewjones View Post
Then what is the primary purpose...???
Well - not that it matters - but it's an addition to
software which I have had developed for me

Quote:
Originally Posted by drewjones View Post
I spend the hours it takes to learn the simplest things....

I don't rely on others information...
But I'm not planning to become a software developer
and won't need to use this information again.

The hours lost in getting this solution could
be put to better use. Agree ?


Harvey

Harvey Segal is offline   Reply With Quote
Old 11-24-2008, 11:18 AM   #12
Banned
War Room Member
 
Join Date: Mar 2008
Location: Long Beach, California
Posts: 112
Thanks: 29
Thanked 7 Times in 7 Posts
Social Networking View Member's Myspace Profile  View Member's FaceBook Profile  View Member's Twitter Profile 
Contact Info
Send a message via AIM to drewjones Send a message via Yahoo to drewjones Send a message via Skype™ to drewjones
Default Re: I need some php code

Agreed...

take a look at this....

PHP: strtok - Manual
drewjones is offline   Reply With Quote
Old 11-24-2008, 11:53 AM   #13
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by drewjones View Post
take a look at this....

PHP: strtok - Manual
Thanks - I've had a quick look but not sure if that will
help as it would extract the text outside of the tokens as
well as inside

Harvey

Harvey Segal is offline   Reply With Quote
Old 11-24-2008, 08:10 PM   #14
Active Warrior
 
Join Date: Jun 2004
Location: , , USA.
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Contact Info
Send a message via Yahoo to n2links
Default Re: I need some php code

I stole this from some open source code. It may give you and idea of how to proceed.

function fill($template, $values) {

if (sizeof($values) != 0) {
// go through all the values
for(reset($values); $key = key($values); next($values)) {
$template = str_replace("{$key}",$values[$key],$template);
}
}

// remove non matched template-tags
return preg_replace('/{[a-zA-Z]+}/','',$template);
}

Jim Gray
http://quikonnex.com
Alternative to Email Publishing
n2links is offline   Reply With Quote
Old 11-25-2008, 03:24 AM   #15
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by n2links View Post
I stole this from some open source code. It may give you and idea of how to proceed.

function fill(, ) {

if (sizeof() != 0) {
// go through all the values
for(reset(); = key(); next()) {
= str_replace("",,);
}
}

// remove non matched template-tags
return preg_replace('/{[a-zA-Z]+}/','',);
}
Jim

Thank you for that.

I only know php basics so that's too difficult for me
to follow but someone else reading this thread
might be able to use it


Harvey

Harvey Segal is offline   Reply With Quote
Old 11-25-2008, 04:17 AM   #16
Mr. Cueball
War Room Member
 
Thomas Belknap's Avatar
 
Join Date: Sep 2006
Location: , , .
Posts: 4,507
Thanks: 196
Thanked 1,187 Times in 589 Posts
Default Re: I need some php code

Harvey, I am not a PHP coder but do know other programming languages.

If you look at Sam's, you can change the Harvey and Sam to variables on the page. That would give you the runtime names instead of Harvey and Sam.


Does that make sense?

Thomas
Thomas Belknap is offline   Reply With Quote
Old 11-25-2008, 04:38 AM   #17
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by Thomas Belknap View Post
Harvey, I am not a PHP coder but do know other programming languages.

If you look at Sam's, you can change the Harvey and Sam to variables on the page. That would give you the runtime names instead of Harvey and Sam.


Does that make sense?
Thanks Thomas but it still does not help

The problem, as mentioned, is that abc, def etc are not known in advance

Harvey

Harvey Segal is offline   Reply With Quote
Old 11-25-2008, 04:41 AM   #18
Mr. Cueball
War Room Member
 
Thomas Belknap's Avatar
 
Join Date: Sep 2006
Location: , , .
Posts: 4,507
Thanks: 196
Thanked 1,187 Times in 589 Posts
Default Re: I need some php code

Quote:
Originally Posted by Harvey.Segal View Post
Thanks Thomas but it still does not help

The problem, as mentioned, is that abc, def etc are not known in advance

Harvey

Exactly, the variables can be from a database or from something the person entered into a form. The information must have been sent if you want to use it. You just plug that information into what Sam wrote. This is done when the website is running.


For example.

A person can enter in a number between 1 and 10.

I don't know what they will enter in, I just know that there will be a number in a form field called num.


So I take Sam's code and do something like this.
= str_replace("{abc}", num, ); <--- you see I inserted num there.


If you don't know the values when the script runs then it will never work.

Thomas
Thomas Belknap is offline   Reply With Quote
Old 11-25-2008, 04:43 AM   #19
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Amazing

I've got the answer from tek-tips.com

Basically one command can do it


================================================== ==========
$s='Hello {abc}, goodbye {def}';

echo preg_replace_callback('/\{([^{}]+)\}/',whatever,$s);

function whatever($what)
{
return strtoupper($what[1]);
}
================================================== ==========

Harvey Segal is offline   Reply With Quote
Old 11-25-2008, 04:49 AM   #20
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by Thomas Belknap View Post
So I take Sam's code and do something like this.
= str_replace("{abc}", num, ); <--- you see I inserted num there.
But where did you get abc from ?

Harvey

Harvey Segal is offline   Reply With Quote
Old 11-25-2008, 05:48 AM   #21
Mr. Cueball
War Room Member
 
Thomas Belknap's Avatar
 
Join Date: Sep 2006
Location: , , .
Posts: 4,507
Thanks: 196
Thanked 1,187 Times in 589 Posts
Default Re: I need some php code

Quote:
Originally Posted by Harvey.Segal View Post
But where did you get abc from ?

Harvey
You tell me. lol

From the url string, if that is what you meant in the OP. If you have more than one token with the url then you wouldn't be able to use Sam's idea.

Thomas
Thomas Belknap is offline   Reply With Quote
Old 11-25-2008, 06:04 AM   #22
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by Thomas Belknap View Post
You tell me. lol

From the url string, if that is what you meant in the OP. If you have more than one token with the url then you wouldn't be able to use Sam's idea.
But the value abc is unknown beforehand
So you cannot have code which mentions it
e.g

str_replace("{abc}", num, )

Now if you are saying that abc should be replaced by a variable
and instead of one str_replace command we should loop through
the string then that is exactly the problem I am trying to solve.

However just after you posted I found the solution above.

Harvey

Harvey Segal is offline   Reply With Quote
Old 11-27-2008, 11:09 AM   #23
Warrior Member
 
Join Date: Nov 2008
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Default Re: I need some php code

Quote:
Originally Posted by n2links View Post
I stole this from some open source code. It may give you and idea of how to proceed.

function fill(, ) {

if (sizeof() != 0) {
// go through all the values
for(reset(); = key(); next()) {
= str_replace("",,);
}
}

// remove non matched template-tags
return preg_replace('/{[a-zA-Z]+}/','',);
}
Thanks! It helps! I think soon I'll become so good programmer!!

andr102 is offline   Reply With Quote
Old 11-27-2008, 03:07 PM   #24
HyperActive Warrior
War Room Member
 
rwil02's Avatar
 
Join Date: May 2005
Location: Auckland, New Zealand.
Posts: 241
Thanks: 0
Thanked 13 Times in 12 Posts
Social Networking View Member's Twitter Profile 
Default Re: I need some php code

OK. Not a PHP programmer, but I can manage.
Here is the code stolen from above.
===========================
$s='Hello {abc}, goodbye {def}';

echo preg_replace_callback('/\{([^{}]+)\}/',whatever,$s);

function whatever($what)
{
return strtoupper($what[1]);
}

==============================

Wht's happening:

preg_replace_callback = the first funny string basically says match everything in {} that is not a curly bracket that gets your ABC or DEF

whatever is the name of a function

$s is the string you are looking through "Hello {abc}, goodbye {def}" in the example

So in the function "whatever" this code returns the uppercase version of the string that was matched (abc or def converts to ABC or DEF)

so you would replace the functionality in there with something like

$lookup = strtoupper($what[1]); //Assumes you want a case insensitive lookup
//Use $lookup to find the value you want, such as using a map variable or looking the value up in the DB (which would be very slow if you do it repeatedly).


===========================
$s='Hello {abc}, goodbye {def}';

echo preg_replace_callback('/\{([^{}]+)\}/',replace_token_with,$s);

function replace_token_with($what)
{
$lookup = strtoupper($what[1]);
return get_value_of($lookup);
}

==============================

rwil02 is offline   Reply With Quote
Old 11-27-2008, 05:50 PM   #25
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by rwil02 View Post
//Use to find the value you want, such as using a map variable or looking the value up in the DB (which would be very slow if you do it repeatedly).
Thanks Roger, though I have now adapted and implemented the code
in my program.

If you are interested to know what it was for it was to
allow cloaked links in a PDF rebranding tool.

I need to change {{url}} to decodeurlscript.php?u = url
throughout a page of HTML.

You can see it in action here

Try the Demo (Item 3) at
Login


Harvey

Harvey Segal is offline   Reply With Quote
Old 11-27-2008, 06:13 PM   #26
HyperActive Warrior
War Room Member
 
rwil02's Avatar
 
Join Date: May 2005
Location: Auckland, New Zealand.
Posts: 241
Thanks: 0
Thanked 13 Times in 12 Posts
Social Networking View Member's Twitter Profile 
Default Re: I need some php code

Needs a login
So you have wanted something like

function replace_token_with($what)
{

return "http://www.example.com/decodeurlscript.php?u=" + urlencode($what[1]);
}

I've never found doing in place substitutions inside PDF files to work well, especially since it requires uncompressed PDFs.

How do you do it? Just a simple replace, or modify a base (eg postscript) fiel and generate from that?

rwil02 is offline   Reply With Quote
Old 11-27-2008, 06:24 PM   #27
Mr SuperTips
War Room Member
 
Join Date: Jul 2002
Location: United Kingdom.
Posts: 3,755
Thanks: 39
Thanked 631 Times in 263 Posts
Default Re: I need some php code

Quote:
Originally Posted by rwil02 View Post
Needs a login
Login as a NEW affiliate

Quote:
Originally Posted by rwil02 View Post
So you have wanted something like

function replace_token_with()
{

return "http://www.example.com/decodeurlscript.php?u=" + urlencode();
}
That's right

Quote:
Originally Posted by rwil02 View Post
How do you do it? Just a simple replace, or modify a base (eg postscript) fiel and generate from that?
I modify an HTML file and create the PDF from that
See
http://www.ultimaterebrander.com/

Harvey

Harvey Segal is offline   Reply With Quote
Reply

  WarriorForum - Internet Marketing Forums > Warrior Support Forums > Programming Talk

Tags
code, php

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -6. The time now is 03:26 PM.