Page 1 of 1

Replacing string array elements

Posted: Thu Dec 13, 2012 1:54 am
by lance_kidd
Sorry for the title of this, as it's probably not the right lingo :crazy: My PHP skills are limited to being able to do what I want with my wordpress blog, but that's about all.

I want to place a string, bible verses specifically, and have something that pulls all the verse numbers from the string, wrap them in HTML tags that I style with css, and then put everything back where it goes, with the numbers wrapped in the tags. I found this online and it pulls the numbers out, and I've gotten them wrapped in the HTML tags, but how do I output everything back where it was, leaving the tags that I added?

I know people get irritated when other people ask them to write their code, but I'm just curious if someone knows of a simple function that I'm aware of. I've spent several hours searching online, but some of it is just over my head at the moment.

Code: Select all

//Creates the function to extract numbers
function extract_numbers($string) {
preg_match_all('/([\d]+)/', $string, $match);

return $match[0]; }
I can figure out how to output just the numbers with the tags using a foreach loop, but I have no idea how to output it back where it went.

Re: Replacing string array elements

Posted: Thu Dec 13, 2012 2:28 am
by requinix
preg_replace() or preg_replace_callback() with regular expressions will be the way to go.

Can you be very specific about what "all the verse numbers" look like? numbers+colon+numbers? Do they need book names? Searches for the words "verse" or "chapter"? If you can nail down the exact rules for what constitutes an inline verse number then it'll be (relatively) straightword to write a regex that matches it.

Re: Replacing string array elements

Posted: Thu Dec 13, 2012 5:19 am
by lance_kidd
What I've been doing is, for example, just on the parables of Jesus, I manually wrapped all of the verse numbers with <span class="ref"></span>, and then styled them. It's just numbers though, no semi-colons, hyphens, etc.

[text]<p><span class="ref">1</span> On that day Jesus went out of the house, and sat by the seaside. <span class="ref">2</span> Great multitudes gathered to him, so that he entered into a boat, and sat, and all the multitude stood on the beach. <span class="ref">3</span> He spoke to them many things in parables, saying, <span class="red">“Behold, a farmer went out to sow. <span class="ref">4</span> As he sowed, some seeds fell by the roadside, and the birds came and devoured them. <span class="ref">5</span> Others fell on rocky ground, where they didn’t have much soil, and immediately they sprang up, because they had no depth of earth. <span class="ref">6</span> When the sun had risen, they were scorched. Because they had no root, they withered away. <span class="ref">7</span> Others fell among thorns. The thorns grew up and choked them. <span class="ref">8</span> Others fell on good soil, and yielded fruit: some one hundred times as much, some sixty, and some thirty. <span class="ref">9</span> He who has ears to hear, let him hear.”</span></p>[/text]

This is how sites like Bible Gateway and others output there scriptures. I know it would probably be better to use a plug-in to do this instead of having all of these HTML elements stored for each post in the database, but for now, I've just been manually doing it, because my site really isn't about scripture look-ups and things like that. So I wanted to put the text in the form and then have it output something like what I have above, then I would copy and paste it to the Wordpress editor.

It's taken me some time to manually wrap each number with the HTML span tags. But on another note, I wanted to do this to get some practice. I took one little PHP class in college and it was so basic that it was a waste of time really, but for what I do, PHP is very handy to know, so I need practice too.

The function I used in the first post does successfully grab the numbers out of any text that I have tried and gathers them in an array, but I have no idea what to do next :banghead:

Thank you again!

Re: Replacing string array elements

Posted: Thu Dec 13, 2012 12:16 pm
by requinix
Can you guarantee that the only numbers (with actual digits) in the text will be a verse number? Like what about Genesis 9:28
28 After the flood Noah lived 350 years.
If not then it gets more complicated. Could you guarantee that the first thing in the string will be a verse number and every sequential number also a verse number? That should drastically cut down any risk of false positive, perhaps to zero. Really though you should be storing each verse individually - that would make everything incredibly easy.

Re: Replacing string array elements

Posted: Sat Dec 15, 2012 3:18 am
by lance_kidd
I guess there would be a time or two that I ran across one, but as it is, I would catch anything that may be incorrectly targeted as a verse. It would just be handy to, on occassions if I did quote a large section of text such as entire chapter, not to have to manually wrap each verse that it contained. I guess more than having a program that run perfectly, I am just wanting to learn.

I'll keep digging though.