473,387 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

subtract string between two tags

bilibytes
128 100+
Hi all!

i am trying to figure how to subtract some string which is between two other strings:

i have:
for(lots of times){
some unuseful text and tags...
<aloha some=text bbbbbbbbb>string</aloha>
some text and other tags that i dont need.
<aloha some=textDifferent bbbbbbbb> stringasdf</aloha>
}


so i noticed that the tags are partly similar.

it would then be great to say:

subtract( $content, startTag='bbb>', endTag='</aloha>')
storing the resulting strings into array $content

if it was a xml document i would parse it, but it is an html with some errors...


i have found this by googleing:

Expand|Select|Wrap|Line Numbers
  1. Answer: How do I extract all text between two keywords like start and end? contributed by Punto
  2. ($content) = $string =~ m/ start (.*) end /;
  3. That will get the stuff between " start " and " end " into $content.
but this doesn't look to be much of PHP...

if you have any suggestion please let me know

thankyou

bili
Nov 4 '08 #1
4 6992
code green
1,726 Expert 1GB
I would suggest using the explode() function.
Arrays have more powerful manipulation functions than strings.
Nov 4 '08 #2
bilibytes
128 100+
thanks for replying, but the problem is that there are man params i would like to pass to the function, not just one as explode accepts.

explode($stringDelimiter, $string).
it is ok for $string like: asdf | asdf| dfaksdfjh | asdfasdfkjh | sdfg
then should be done explide(' | ', $string)

but what if you have : asdf <a first substring <b second substring ?c third substring <b fourth substring ?c

if i just want to get the values between <b and c? tags, i cant use explode() because it accepts only one delimiter...

if you have any other solution

thanyou very much

bili
Nov 4 '08 #3
Atli
5,058 Expert 4TB
if i just want to get the values between <b and c? tags, i cant use explode() because it accepts only one delimiter...
You can use it twice:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $str = "<a first <b second <c third <d fourth";
  3.  
  4. $leading = explode("<b", $str);
  5. $following = explode("<c", $leading[1]);
  6.  
  7. $output = trim($following[1]);
  8.  
  9. // $output == second
  10. ?>
  11.  
You get the idea?

Although, for complex strings you may be better of using regular expressions.
For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $str = "<a first <b second <c third <d fourth";
  3. $regex = '/.*\<b (.*?) <c.*/i';
  4.  
  5. if(preg_match($regex, $str, $matches)) {
  6.   echo $matches[1]; // == second
  7. }
  8. else {
  9.   echo "No match";
  10. }
  11. ?>
  12.  
They are not the easiest thing to get used to, but once you do, they can save a lot of time.
Nov 4 '08 #4
bilibytes
128 100+
ok, so the expert was right, i just didn't see the potential of his answer.

but i am trying to learn these regexp, hope i'll do it!

for all this, thank you very much!
Nov 5 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Hans Gruber | last post by:
Hi all, Here`s a problem I have been working on for a while, but can`t seem to solve satisfactory. I have a database with blog entries. Because each of those entries has a variable length...
2
by: Tom | last post by:
I'm trying to find a good, solid function that sanitizes string values for form validation. The user notes on php.net offer the following: ...
8
by: david | last post by:
Hello, Is the code below correct ? I always get an seg fault on execution and the debugger points to c_str() ? Basically i want to convert the value std::string to char * within the iterator...
8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
4
by: lkrubner | last post by:
I'd like to write a PHP script to be used from the command line on a Unix machine. I'd like for the script to put together a string, turn it into a web page, print it, then return control the...
7
by: Jimbo | last post by:
Anyone know how I can subtract 12 months from the current date?
3
by: ReidarT | last post by:
I have a javascript where I try to add 1 to a value through a button and show the value in a textbox and subtract 1 from another button and show the result in the same textfield. This seems to...
3
by: Alex | last post by:
Hello. First, with AJAX I will get a remote web page into a string. Thus, a string will contain HTML tags and such. I will need to extract text from one <span> for which I know the ID the inner...
3
by: Arne Gemsa | last post by:
Hi, I want to get a starttime. To get this I have to subtract the runtime from a unit e.g. 08:40:15 from a date e.g. 2007-03-29 16:48:30. So the starttime is 2007-03-29 08:08:15. Is there any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.