The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - What is the best function to replase these strings?
Discuss What is the best function to replase these strings? in the PHP Development forum on Dev Shed. What is the best function to replase these strings? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 31st, 2012, 11:41 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 11
Time spent in forums: 2 h 40 m
Reputation Power: 0
|
|
|
PHP-General - What is the best function to replase these strings?
Hi
i have a string such as this :
is there any php function to replace the first red one to "#" and the last red one to "/#"
?
or i have to write an algorithm?
Regards
|

October 31st, 2012, 12:07 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Are they all that one string? Or do you mean for the black text to be variable?
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

October 31st, 2012, 12:12 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 15
Time spent in forums: 3 h 49 m 21 sec
Reputation Power: 0
|
|
|
what is @ for? in php, if you want to replace a string use str_replace()
Syntax:
str_replace(find,replace,string,count)
|

October 31st, 2012, 12:22 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 11
Time spent in forums: 2 h 40 m
Reputation Power: 0
|
|
Quote: | Are they all that one string? Or do you mean for the black text to be variable? |
not actually !
i have numbers of string such as these :
Code:
@some text here@
!some text here!
# some text here #
etc.
and want to replace first red one and last red one to other character.(html tags)
Quote: | what is @ for? in php, if you want to replace a string use str_replace() |
i see,but it can't do it.it is a special form of replace.
|

October 31st, 2012, 12:28 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
So now you've changed your bounding characters as well?
What is the actual problem? You have strings WHERE, bounded by WHAT, and containing WHAT?
Are these strings in a database, or just randomly in a big file?
What are the bounding characters? Just #? #, @, !? Any special character?
What can be in the middle of these strings? Whitespace? Other special characters? Other bounded blocks?
What do you want them to look like?
|

October 31st, 2012, 12:34 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Replace the red characters? If you copy/pasted your example then you're using BBCode.
PHP Code:
$newstring = preg_replace('/^[color=red].[/color](.*)[color=red].[/color]/i', '#$1/#', $oldstring);
Not sure if you want to keep the red coloring though.
|

October 31st, 2012, 12:40 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 11
Time spent in forums: 2 h 40 m
Reputation Power: 0
|
|
Quote: | Originally Posted by ManiacDan So now you've changed your bounding characters as well?
What is the actual problem? You have strings WHERE, bounded by WHAT, and containing WHAT?
|
I don't know there is any function in php do that
Quote: | Originally Posted by ManiacDan
Are these strings in a database, or just randomly in a big file?
|
No,this is a text file.not in database
Quote: | Originally Posted by ManiacDan
What are the bounding characters? Just #? #, @, !? Any special character?
|
no just these and + some character such as '*' , 'bq.' etc.
Quote: | Originally Posted by ManiacDan
What can be in the middle of these strings? Whitespace? Other special characters? Other bounded blocks?
|
any change.for example. i want to replace "!" with =>> '<img src="' plus follow text and last "!" replace with =>> ' "/> '
Quote: | Originally Posted by ManiacDan
What do you want them to look like?
|
these are murkup editor codes,i want to convert it to tinyemce. thats it
and thanks for your reply
|

October 31st, 2012, 12:46 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
What happens when your content contains multiple email addresses or multiple questions?
Look at this post right here, do you see the problem?
There's two question marks. Do you really want to remove them both and make my second sentence red or whatever?
If you're trying to make a markup parser, you'll have to do it by hand. They are very, very complicated. You have to step through the string one character at a time looking for your markup, keeping track of your depth in the string and which tags you've already found. Since your "tags" are random, and variable length, you have to also keep a list of all possible tags, and make sure that you match closing tags case-insensitively.
Look into how to write a markup parser, or check out the source code of the bbcode parsers.
|

October 31st, 2012, 12:53 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 11
Time spent in forums: 2 h 40 m
Reputation Power: 0
|
|
Quote: | Originally Posted by ManiacDan What happens when your content contains multiple email addresses or multiple questions?
Look at this post right here, do you see the problem?
There's two question marks. Do you really want to remove them both and make my second sentence red or whatever?
If you're trying to make a markup parser, you'll have to do it by hand. They are very, very complicated. You have to step through the string one character at a time looking for your markup, keeping track of your depth in the string and which tags you've already found. Since your "tags" are random, and variable length, you have to also keep a list of all possible tags, and make sure that you match closing tags case-insensitively.
Look into how to write a markup parser, or check out the source code of the bbcode parsers. |
thanks
in my point of view , i have to write my own functions
regards
|

October 31st, 2012, 01:03 PM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
|
Use a regex and bound to '^' and '$' for the start and end of the string
or use substr_replace to replace the first and last symbols in the string
or use substr to pull all but the first and last characters of the string ...
__________________
--Ax
without exception, there is no rule ...
Handmade Irish Jewellery
Targeted Advertising Cookie Optout (TACO) extension for Firefox
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones
 
09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski
Deta vil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
BIT COINS ANYONE
|

October 31st, 2012, 01:06 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
These strings exist mixed throughout a big text file. It's hard to tell because he absolutely refuses to show you some actual data, instead he keeps listing fake examples.
|

October 31st, 2012, 01:07 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 11
Time spent in forums: 2 h 40 m
Reputation Power: 0
|
|
Quote: | Originally Posted by Axweildr Use a regex and bound to '^' and '$' for the start and end of the string
or use substr_replace to replace the first and last symbols in the string
or use substr to pull all but the first and last characters of the string ... |
oh great !
is there any exact example?
thanks for your reply.
|

October 31st, 2012, 01:33 PM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
|
You can look these functions up on http://php.net, or you could help us to help u by providing some live data
The initial sample you gave is some badly formed php code, because of the multiple closing braces on the same line, if we deliver what it is you initially requested there's a very good chance your entire codebase will be rendered less than useless, and if you're doing that in tinymce then you have bigger issues than that.
default: break; <-- pointless
|

October 31st, 2012, 01:55 PM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
|
Sample of substr_replace
php Code:
Original
- php Code |
|
|
|
Use with extreme caution, and back up your data first ...
|

October 31st, 2012, 03:21 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Note that everything Axweildr is saying assumes you have these strings just as you've presented, them. That is, "@hello@" is by itself in its own file or database field with no other content around it. You have made it clear that's not how you have your data, so this will not work for you.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|