Jump to content

replace numbers


golles

Recommended Posts

Hey, I have a (generated) table with some numbers I'd like to change to their name (database)

 

the number is in this format:

<td>/978100/</td>

or

<td>/841110/841115/</td>

 

I think it could be done with preg_match_all, but I have no clue how where to search on

 

$search = "???";

preg_match_all($search, $table, $matches);

foreach ($matches as $m){
//replace the matches with their names here
}

 

Or is this really stupid?

 

Thanks in advance!

Link to comment
Share on other sites

Why replace them? You can't just display the names?

No the table is not generated by me. its from an application.

 

This did allmost the trick: it only not does the ones like /841110/841115/

$search = "'/([^<]*)/'s";

result:

Array ( [0] => 942480 [1] => 941750 [2] => 945955 [3] => 941230 [4] => 978100 [5] => 975600 [6] => 824110 [7] => 841290/842030 
[8] => 841010 [9] => 841110/841115 ) 

 

how could the $search do them all?

Link to comment
Share on other sites

It's because the pattern is by default greedy, there are a couple of solutions, but the simplest one is probably to just add a forward slash to the negated character class...

 

$search = "'/([^</]*)/'s";

Link to comment
Share on other sites

that gave the same result

 

this is the complete table btw

 

<table>
	<tr>
		<td>0</td>
		<td>-15.75</td>
		<td>/942480/</td>
		<td>26.37</td>
	</tr>
	<tr>

		<td>1</td>
		<td>-10.567</td>
		<td>/941750/</td>
		<td>25.39</td>
	</tr>
	<tr>
		<td>2</td>

		<td>-12.267</td>
		<td>/945955/</td>
		<td>25.07</td>
	</tr>
	<tr>
		<td>3</td>
		<td>-12.233</td>

		<td>/941230/</td>
		<td>25.07</td>
	</tr>
	<tr>
		<td>4</td>
		<td>-5.683</td>
		<td>/978100/</td>

		<td>25.07</td>
	</tr>
	<tr>
		<td>5</td>
		<td>-1.183</td>
		<td>/975600/</td>
		<td>24.2</td>

	</tr>
	<tr>
		<td>6</td>
		<td>-3.667</td>
		<td>/824110/</td>
		<td>23.53</td>
	</tr>

	<tr>
		<td>7</td>
		<td>-2.15</td>
		<td>/841290/842030/</td>
		<td>22.91</td>
	</tr>
	<tr>

		<td>8</td>
		<td>-0.583</td>
		<td>/841010/</td>
		<td>22.68</td>
	</tr>
	<tr>
		<td>9</td>

		<td>-1.033</td>
		<td>/841110/841115/</td>
		<td>22.3</td>
	</tr>
</table>

Link to comment
Share on other sites

There are 10 numbers of the format that you are looking for in the HTML from the last post, your previous post showed 10 matched numbers. What's the problem?

 

Edit: If you're looking to match 12345/67890 as two numbers, then you could do something like the following.

 

$search = '~(?<=/)\d+(?=/)~'

 

This uses a lookbehind* and lookahead* to search for numbers surrounded by forward slashes.

 

* More info: lookaround :shy:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.