0

What's the best way to get text from a string. I have a LONGTEXT field in the db and currently is:

<ul>
 <li>Winner on Laps #153 LemonAid Racing (1996 Geo Metro, Davenport Iowa)</li>
 <li>Winner on Index of Effluency #42 J CrewD (1985 Jagaur XJ6, Knoxville Tn)     </li>
 <li>Winner, Class A (The Good) #153 LemonAid Racing (1996 Geo Metro, Davenport Iowa)</li>
 <li>Winner, Class B (The Bad) #128 Duff Beer (1994 Honda Civic, Raleigh NC and Atlanta GA)</li>
 <li>Winner, Class C (The Ugly) #64 Team Fairlylame (1964 Ford Fairlane, Savannah GA)</li>
 <li>Organizer's Choice #54 Knoxvegas Lowballers (1998 Ford Contour SVT, Knoxville TN)</li>
 <li>Most Heroic Fix #40 Mock Grass Racing (1998 Kia Sephia, Columbia SC)</li>
 <li>Judges' Choice #85 Apocalyptic Racing (1978 Toylet Celica, St Louis mo)     </li>
 <li>Judges' Choice #2 #17 Fireball Racing (1991 Ford Escort, Aurora IL)</li>
</ul>

1 Say I wanted to get the strings "1996 Geo Metro, Davenport Iowa", "1985 Jagaur XJ6, Knoxville Tn".. etc... from the entire string (the text inside the parenthesis).What is the best way to do so?

  1. Say I wanted to get all the text in the 'li' up to the symbol "#", is that possible?

Thanks in advance

2
  • 1
    Try to use regex for this. Commented May 31, 2017 at 19:09
  • use preg_match_all Commented May 31, 2017 at 19:12

2 Answers 2

3

You can try using Regular Expressions.

For example, to find (the text inside the parenthesis) you can use the following regex:

/(?<=\()([^\)]+)(?=\))/

and to get all the text in the 'li' up to the symbol "#" you can use:

/(?<=<li>).*(?=#)/

Perform a regular expression match on PHP

Awesome regex resource!

Sign up to request clarification or add additional context in comments.

8 Comments

Why do you escape #? Shouldn't you start after <li>?
Going to give these a try! Thank you!
No problem. If this answer helped you to solve your problem, mark it as accepted. Good luck! :) @KenRyan
hi @rafascar how could I get the "team name" from "Winner on Laps #153 LemonAid Racing (1996 Geo Metro, Davenport Iowa)." Basically I'd want "LemonAid Racing" from this part. Would it be the string after the first number and before the "("? thanks for your help again!
To get only the texts between parentheses that follow the same pattern as (1996 Geo Metro, Davenport Iowa), for example, you can try this regex: (?<=\()([^\)]+)[,]([^\)]+)(?=\))
|
0

Use preg_match_all

  1. preg_match_all('/\(([^\)]+)\)/', $content, $matches);
  2. preg_match_all('/^[\s]*\<li\>([^#]+)#/m', $content, $matches);

$matches[1] will contain your captured text.

7 Comments

No need to put \s in [...].
Are you sure ? Try using my regex to capture without it. :-)
Why would I, it's simply the same.
Ummm, nope, not really. To elaborate, you can't tell from just looking at the question, but there is actually whitespace between the start of each line and the <li> tag.
Tried. It's the same.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.